Mouse position registering in corner of screen with new input system.

So I am working on a game and I would like to make a gun that follows my mouse. I am using the new input system and It should be pretty easy. But after a couple of days of testing I have decided to ask for help. My problem is that the mouse point that the gun should be pointing at is in the corner of the screen and not in the center. If you need more clarification I can upload videos.

Vector2 pos = playerCamera.ScreenToViewportPoint(Mouse.current.position.ReadValue());
transform.up = pos;

If anyone else comes across this post, I had the same issue. Here was my fix for it:

    public class FollowMouseForUI : MonoBehaviour
    {
        [SerializeField] Canvas myCanvas;
        private Camera cam;
        private Vector2 mousePosition;
        private Vector2 pos;

        private void Awake()
        {
            cam = Camera.main;
        }

        // Update is called once per frame
        void Update()
        {
            RectTransformUtility.ScreenPointToLocalPointInRectangle(myCanvas.transform as RectTransform, Mouse.current.position.ReadValue(), myCanvas.worldCamera, out pos);
            transform.position = myCanvas.transform.TransformPoint(pos);
        }
    }
1 Like