Search Unity

Resolved Mouse click position is inaccurate using the new input system

Discussion in 'Input System' started by gameGator, Feb 16, 2023.

  1. gameGator

    gameGator

    Joined:
    Sep 21, 2018
    Posts:
    14
    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 :

    Code (CSharp):
    1. public void Shoot(InputAction.CallbackContext context)
    2.     {
    3.         if (context.started)
    4.         {
    5.             Debug.Log("Shoot!");
    6.             Vector3 pointPosition3d = new Vector3(pointPosition.x, pointPosition.y, Camera.main.nearClipPlane);
    7.             Vector3 worldPosition = Camera.main.ScreenToWorldPoint(pointPosition3d);
    8.             Vector2 direction = (new Vector2(worldPosition.x, worldPosition.y) - new Vector2(this.transform.position.x, this.transform.position.y));
    9.             direction.Normalize();
    10.             RaycastHit2D raycastHit2D = Physics2D.Raycast(new Vector2(this.transform.position.x, this.transform.position.y), direction);
    11.             GameObject lr = Instantiate(linePrefab, this.transform);
    12.             LineRenderer temp = lr.GetComponent<LineRenderer>();
    13.             temp.enabled = false;
    14.             temp.SetPosition(0, new Vector2(this.transform.position.x, this.transform.position.y));
    15.  
    16.             if (raycastHit2D.collider != null) {
    17.                 temp.SetPosition(1, raycastHit2D.point);
    18.                 Debug.Log("I hit something : " + raycastHit2D.collider.gameObject.tag);
    19.             } else
    20.             {
    21.                 temp.SetPosition(1, direction * 2);
    22.             }
    23.             temp.enabled = true;
    24.         }
    25.     }
    the pointPosition variable is calculated by calling the following method in every Update call :

    Code (CSharp):
    1. pointPosition = pointAction.ReadValue<Vector2>();
    and the action initialization code in the Start method :


    Code (CSharp):
    1. input = GetComponent<PlayerInput>();
    2. moveAction = input.actions["Move"];
    3. input.actions.FindActionMap("UI").Enable();
    4. pointAction = input.actions["Point"];
    Thanks in advance for any pointers! mouse position.JPG
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    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.
     
    gameGator likes this.
  3. gameGator

    gameGator

    Joined:
    Sep 21, 2018
    Posts:
    14
    @MelvMay that would be really helpful!
    Thanks!
     
    MelvMay likes this.
  4. gameGator

    gameGator

    Joined:
    Sep 21, 2018
    Posts:
    14
    @MelvMay Please move this post to the correct thread(s) as required. Thanks a lot for helping me out here!
     
  5. gameGator

    gameGator

    Joined:
    Sep 21, 2018
    Posts:
    14
  6. gameGator

    gameGator

    Joined:
    Sep 21, 2018
    Posts:
    14
    Here are my camera settings in case this helps debug the issue.


    cameraSettings.JPG
     
  7. gameGator

    gameGator

    Joined:
    Sep 21, 2018
    Posts:
    14
    Resolved:
    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 :oops:)
    Code (CSharp):
    1.  else
    2.             {
    3.                 temp.SetPosition(1,new Vector2(this.transform.position.x + (5f*direction.x), this.transform.position.y + (5f*direction.y)));
    4.             }
     
    MelvMay likes this.