title: How to change hit point to crosshair?
body: <p>Below script uses a function whereas there first is a check if there is a object in range and if it, it is.<br>
  On pressing mouse key, the projectile is shooting towards the pivot point of that said object. I want the projectile to </p>
  
  <ol>
  <li>always being able to fire (indifferent if the object is in range) and </li>
  <li>shoot towards the crosshair (to the screen midpoint) and NOT towards the object pivot point. </li>
  </ol>
  
  <p>I am a novice at coding and I can't see what code to remove and what to add.</p>
  
  <pre><code>    using System.Collections;
      using System.Collections.Generic;
      using UnityEditor;
      using UnityEngine;
      using UnityEngine.Events;
  
      public struct ShootHit
      {
      public GameObject gameObject;
      public Vector3 point;
      }
  
      [System.Serializable]
      public class UnityEventShootHit : UnityEvent&lt;ShootHit&gt; { }
  
      [DisallowMultipleComponent, AddComponentMenu("(つ♥v♥)つ/Useables/Shoot")]
      public class ShootComponent : MonoBehaviour
      {
      [Header("Input")]
      [Tooltip("Data that determines the input of player actions")]
      [SerializeField] private InputProfile _inputProfile;
      // [SerializeField] private float _range = 100.0f;
  
      /* [Header("Sight Values")]
       [Tooltip("How far the the sight can reach")]
       public float sightRadius = 1f;
       [Range(0f, 360f)]
       public float fieldOfViewAngle = 100f;*/
  
      [Header("Charge-up")]
      [SerializeField] private float _chargeupTime = 0.5f;
      private bool _isChargingPrimary = false;
      private bool _isChargingSecondary = false;
  
      [Header("Aim Assist")]
      [SerializeField] private LayerMask _aimAssistLayerMask;
  
      public float aimAssistRadius = 30.0f;  // radius
      [Range(0.0f, 360.0f)]
      public float aimAssistMaxAngleToAssist = 45.0f;  // angle
  
      private ShootHit? _target;
  
      //publics
      public Transform shootOrigin;
  
      [Header("Events")]
      public UnityEventShootHit OnPrimaryFire;
      public UnityEvent OnPrimaryFireStart;
      public UnityEvent OnPrimaryFireStop;
  
      public UnityEventShootHit OnSecondaryFire;
      public UnityEvent OnSecondaryFireStart;
      public UnityEvent OnSecondaryFireStop;
  
      private void Start()
      {
          if (_inputProfile == null) Debug.LogError(gameObject.name + " does not 
          have a player input");
      }
  
       private void Update()
       {
           // Remove target if object is too far away
           if (_target.HasValue)
           {
              if (Vector3.Distance(_target.Value.gameObject.transform.position, 
           transform.position) &gt; aimAssistRadius)
              {
                  _target = null;
              }
           }
  
           if (_inputProfile.GetPrimaryFireButtonDown())
           {
              StopCoroutine(ChargeUpBeforeFireSecondary());
  
              if (!_isChargingPrimary)
              {
                  StartCoroutine(ChargeUpBeforeFirePrimary());
              }
           }
  
           else if (_inputProfile.GetSecondaryFireButtonDown())
           {
              StopCoroutine(ChargeUpBeforeFirePrimary());
  
              if (!_isChargingSecondary)
              {
                  StartCoroutine(ChargeUpBeforeFireSecondary());
              }
          }
  
           if (_inputProfile.GetPrimaryFireButton() || 
          _inputProfile.GetSecondaryFireButton())
          {
              if (!_target.HasValue) _target = GetObjectClosestToAim();
  
              if (_inputProfile.GetPrimaryFireButton())
              {
                  OnPrimaryFire.Invoke(_target.Value);
              }
              if (_inputProfile.GetSecondaryFireButton())
              {
                  OnSecondaryFire.Invoke(_target.Value);
              }
  
          }
          else
          {
              _target = null;
          }
  
          if (_inputProfile.GetPrimaryFireButtonUp())
              OnPrimaryFireStop.Invoke();
          if (_inputProfile.GetSecondaryFireButtonUp())
              OnSecondaryFireStop.Invoke();
      }
  
      /// &lt;summary&gt;
      /// Finds the object within range closest to the players forward-vector 
      using _aimAssistLayerMask.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;Returns object closest to aim if any object is found, else 
      returns null.&lt;/returns&gt;
      ShootHit? GetObjectClosestToAim()
      {
          // Raycast
          RaycastHit hit;
          if (Physics.Raycast(shootOrigin.position, Camera.main.transform.forward, 
          out hit, aimAssistRadius, _aimAssistLayerMask))
          {
              if (hit.transform?.GetComponent&lt;IShootTarget&gt;() != null)
              {
                  Debug.Log(hit.transform.name);
  
                  return new ShootHit { gameObject = hit.transform.gameObject, 
      point = hit.point };
              }
          }
  
          float _closestDot = -2f;
          GameObject _closestDotObject = null;
          RaycastHit[] _hit = Physics.SphereCastAll(transform.position, 
          aimAssistRadius, transform.forward, 0, _aimAssistLayerMask, 
          QueryTriggerInteraction.Ignore);
  
          // Get best dot from all objects within range
          for (int i = 0; i &lt; _hit.Length; i++)
          {
              if (_hit[i].transform.gameObject == this.gameObject || 
          _hit[i].transform.GetComponent&lt;IShootTarget&gt;() == null)
                  continue;
  
              Vector3 _dif = _hit[i].transform.position - transform.position;
  
              float _newDot = Vector3.Dot(transform.forward.normalized, 
        _dif.normalized);
              if (_newDot &gt; _closestDot)
              {
                  _closestDot = _newDot;
                  _closestDotObject = _hit[i].transform.gameObject;
              }
          }
          if (!_closestDotObject)
              return null;
  
          // Make sure there are no object in the way of our best-dot-object
          Collider[] colliders = _closestDotObject.GetComponents&lt;Collider&gt;();
          Vector3 point = colliders[0].ClosestPoint(shootOrigin.position);
          float distanceToPoint = Vector3.Distance(shootOrigin.position, point);
          // Get closest collider
          for (int i = 1; i &lt; colliders.Length; i++)
          {
              Vector3 newPoint = colliders[i].ClosestPoint(shootOrigin.position);
              float newDistanceToPoint = Vector3.Distance(shootOrigin.position, 
          newPoint);
              if (distanceToPoint &gt; newDistanceToPoint)
              {
                  point = newPoint;
                  distanceToPoint = newDistanceToPoint;
              }
          }
          RaycastHit _rayhit;
          if (Physics.Raycast(shootOrigin.position, point - transform.position, 
          out _rayhit, aimAssistRadius, _aimAssistLayerMask))
          {
              if (_rayhit.transform.gameObject != _closestDotObject)
              {
                  return null;
              }
          }
  
          Vector3 _vecToClosest = _closestDotObject.transform.position - 
      transform.position;
          if (Vector3.Angle(transform.forward, _vecToClosest) &lt;= 
      aimAssistMaxAngleToAssist)
           {
              return new ShootHit { gameObject = _closestDotObject, point = point 
   };
          }
          else
          {
              return null;
          }
      }
  
       IEnumerator ChargeUpBeforeFirePrimary()
      {
          _isChargingPrimary = true;
          yield return new WaitForSeconds(_chargeupTime);
          _isChargingPrimary = false;
          OnPrimaryFireStart.Invoke();
      }
  
      IEnumerator ChargeUpBeforeFireSecondary()
      {
          _isChargingSecondary = true;
          yield return new WaitForSeconds(_chargeupTime);
          _isChargingSecondary = false;
          OnSecondaryFireStart.Invoke();
      }
  
  
      #if UNITY_EDITOR
      private void OnDrawGizmosSelected()
      {
          if (!Application.isPlaying) return;
  
          Color oldColor = Gizmos.color;
  
          float halfFeildOfView = aimAssistMaxAngleToAssist * 0.5f;
          float coneDirection = -90f;
  
          Quaternion leftRayRotation = Quaternion.AngleAxis(-halfFeildOfView + coneDirection, Vector3.up);
          Quaternion rightRayRotation = Quaternion.AngleAxis(halfFeildOfView + coneDirection, Vector3.up);
  
          Vector3 leftRayDirection = leftRayRotation * transform.right * aimAssistRadius;
          Vector3 rightRayDirection = rightRayRotation * transform.right * aimAssistRadius;
  
          // Green Arc
          Handles.color = new Color(0f, 1f, 0f, 0.25f);
          Handles.DrawSolidArc(transform.position, Vector3.up, leftRayDirection, aimAssistMaxAngleToAssist, aimAssistRadius);
  
          Gizmos.color = oldColor;
  
  
      }
       #endif
  
  }
  `
  </code></pre>
  
  <p>This is the code attached to the projectile</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  [RequireComponent(typeof(Rigidbody))]
  public class ProjectileScript : MonoBehaviour
  {
  
      public GameObject Explosion;
  
      public Transform Target;
  
      Rigidbody _rigidbody;
  
      public float speed = 1.0f;
  
      void Start()
      {
          _rigidbody = GetComponent&lt;Rigidbody&gt;();
  
      }
  
      void FixedUpdate()
      {
          // _rigidbody.AddForce((Target.position - transform.position).normalized, ForceMode.Impulse);
          Collider targetCollider = Target.GetComponent&lt;Collider&gt;();
          Vector3 targetDirection;
          if (targetCollider)
              targetDirection = targetCollider.ClosestPoint(transform.position) - transform.position;
          else
              targetDirection = Target.position - transform.position;
  
          _rigidbody.velocity = targetDirection.normalized * speed;
          if (Vector3.Distance(transform.position, Target.position) &lt; 1.0f)
          {
              //make the explosion
              GameObject ThisExplosion = Instantiate(Explosion, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
  
              //destory the projectile
              Destroy(gameObject);
          }
      }
  
      public void SetTarget(GameObject target)
      {
          this.Target = target.transform;
      }
  
      public void SetTarget(ShootHit hit) =&gt; SetTarget(hit.gameObject);
  
  }
  </code></pre>
  
  <p>This script is how and where the Projectile Spawns. It is attached to an empty gameobject located on the muzzle of the gun. </p>
  
  <pre><code>public class ProjectileSpawner : MonoBehaviour
  {
  
      public GameObject projectileInsert;
  
          public GameObject projectileExtract;
          public float projectileSpeed = 1.0f;
  
          GameObject projectile;
  
          public void Insert(GameObject target)
          {
              if (projectile) return;
  
              projectile = Instantiate(projectileInsert, transform.position, Quaternion.identity);
              ProjectileScript projectileScript = projectile.GetComponent&lt;ProjectileScript&gt;();
              projectileScript.SetTarget(target);
              projectileScript.speed = projectileSpeed;
  
              // Physics.IgnoreCollision(projectile.GetComponent&lt;Collider&gt;(),
              // projectileSpawn.parent.GetComponent&lt;Collider&gt;()); 
  
          }
  
          public void Extract(GameObject target)
          {
              if (projectile) return;
  
              projectile = Instantiate(projectileExtract, target.transform.position, Quaternion.identity);
              ProjectileScript projectileScript = projectile.GetComponent&lt;ProjectileScript&gt;();
              projectileScript.SetTarget(gameObject);
              projectileScript.speed = projectileSpeed;
  
              // Physics.IgnoreCollision(projectile.GetComponent&lt;Collider&gt;(),
              // projectileSpawn.parent.GetComponent&lt;Collider&gt;()); 
  
          }
  
  
      }
  </code></pre>