How could i make my gun move where my cursor is?

I have a mounted gun on my vehicle and I would like it to move where my cursor is.

Edit: After getting more specs I’ve re-done this answer.


There are 2 scripts, The CameraScript and the PointerScript. The CameraScript needs a reference to a PointerScript. And the PointerScript must be attached to a gameobject that is childed to another gameobject. Their world positions need to be the same. When the PointerScript is selected a frustum gizmo will be drawn that shows the approximate field of view the PointerScript can take

CameraScript

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    [RequireComponent(typeof(Camera))]
    public class CameraScript : MonoBehaviour
    {
      public PointerScript myPointer;

      private Camera myCamera;
      private RaycastHit rayHit;
      public Vector3? CurrentPoint { get; private set; }

      private void Awake()
      {
        myCamera = GetComponent<Camera>();
      }

      // Use this for initialization
      void Start ()
      {
        if (myPointer == null)
        {
          Debug.LogError(string.Format("'myPointer' was not set, disabling CameraScript on gameobject: ({0})", name));
          this.enabled = false;
        }
        }

        // Update is called once per frame
        void Update ()
      {
        CurrentPoint = GetNewPosition();
        myPointer.UpdateTarget(CurrentPoint);
      }

      private Vector3? GetNewPosition()
      {
        Ray myRay = myCamera.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(myRay, out rayHit))
        {
          return rayHit.point;
        }
        return null;
      }
    }

PointerScript

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PointerScript : MonoBehaviour
    {
      private Transform tran;
      private Transform parentTran;

      [Tooltip("Maximum degrees this transform will look either side (along the y-axis) of its parents forward")]
      public float yawConeDegrees;

      [Tooltip("Maximum degrees this transform will look up and down of its parents forward")]
      public float pitchConeDegrees;

      private Vector3? currentTarget;
      private Vector3 aimDirection;
      private float returnTime;

      private void Awake()
      {
        tran = transform;
      }

      void Start () {
        if (tran.parent == null)
        {
          Debug.LogError(string.Format("This script 'PointerScript' requires a parent, disabling PointerScript on gameobject: ({0})", name));
          this.enabled = false;
        }

        parentTran = tran.parent;
        }

      void Update ()
      {
        if (currentTarget.HasValue) // If we have a position, rotate towards it
        {
          aimDirection = (currentTarget.Value - tran.position).normalized;
          Vector3 parentForward = parentTran.forward;

          Vector3 yawFlat = aimDirection;
          yawFlat.y = 0;
          yawFlat = yawFlat.normalized;

          Vector3 pitchFlat = aimDirection;
          pitchFlat.x = 0;
          pitchFlat = pitchFlat.normalized;

          float yaw = Vector3.SignedAngle(parentForward, yawFlat, tran.up);
          float pitch = Vector3.SignedAngle(parentForward, pitchFlat, tran.forward);

          if (Mathf.Abs(yaw) <= yawConeDegrees && Mathf.Abs(pitch) <= pitchConeDegrees)
          {
            tran.LookAt(currentTarget.Value, parentTran.up);
          }
        }
        }

      private void OnDrawGizmosSelected()
      {
        if (transform.parent != null)
        {
          Matrix4x4 previous = Gizmos.matrix;
          Gizmos.matrix = transform.parent.localToWorldMatrix;
          Gizmos.DrawFrustum(Vector3.zero,pitchConeDegrees*2, 5,0, yawConeDegrees / pitchConeDegrees);
          Gizmos.matrix = previous;
        }

      }

      public void UpdateTarget(Vector3? newPoint)
      {
        currentTarget = newPoint;
      }
    }