Mouse click position is inaccurate using the new input system

I am trying to draw a line in the direction of mouse click but the line doesnt get drawn at an accurate angle.
Please see attached photo for reference (The red pointers were the mouse positions).
Sometimes the line is drawn above and sometimes below the actual pointer position.

I am using the new input system and using Invoke Unity Events to call the shoot and jump methods.
Here is my code for the shoot function which draws the line along a raycast :

public void Shoot(InputAction.CallbackContext context)
        if (context.started)
            Vector3 pointPosition3d = new Vector3(pointPosition.x, pointPosition.y, Camera.main.nearClipPlane);
            Vector3 worldPosition = Camera.main.ScreenToWorldPoint(pointPosition3d);
            Vector2 direction = (new Vector2(worldPosition.x, worldPosition.y) - new Vector2(this.transform.position.x, this.transform.position.y));
            RaycastHit2D raycastHit2D = Physics2D.Raycast(new Vector2(this.transform.position.x, this.transform.position.y), direction);
            GameObject lr = Instantiate(linePrefab, this.transform);
            LineRenderer temp = lr.GetComponent<LineRenderer>();
            temp.enabled = false;
            temp.SetPosition(0, new Vector2(this.transform.position.x, this.transform.position.y));

            if (raycastHit2D.collider != null) {
                temp.SetPosition(1, raycastHit2D.point);
                Debug.Log("I hit something : " + raycastHit2D.collider.gameObject.tag);
            } else
                temp.SetPosition(1, direction * 2);
            temp.enabled = true;

the pointPosition variable is calculated by calling the following method in every Update call :

pointPosition = pointAction.ReadValue<Vector2>();

and the action initialization code in the Start method :

input = GetComponent<PlayerInput>();
moveAction = input.actions["Move"];
pointAction = input.actions["Point"];

Thanks in advance for any pointers! 8810845--1198636--mouse position.JPG

This is the 2D forum which doesn't deal with the mouse nor the new input system.

You either want this in the scripting forum or the new Input System forum.

I can move your post for you if you want.

1 Like

@MelvMay that would be really helpful!

1 Like

@MelvMay Please move this post to the correct thread(s) as required. Thanks a lot for helping me out here!


Here are my camera settings in case this helps debug the issue.


I was not setting the second point of lineRenderer correctly in case of no collision. Following is the correct way to set it. (Masters in physics here taking 24 hours to figure this out :sweat_smile:)

                temp.SetPosition(1,new Vector2(this.transform.position.x + (5f*direction.x), this.transform.position.y + (5f*direction.y)));
1 Like