Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Mouse position goes out of game view.

Discussion in 'Input System' started by devssun, Feb 23, 2021.

  1. devssun

    devssun

    Joined:
    Feb 1, 2015
    Posts:
    2
    I am getting a mouse position using the New Input System.

    I want to rotate the character to the mouse position. However, the mouse position detects out of the game view, so normal rotation is impossible.

    Input Debugger's 'Lock Input To Game View' option is also ineffective.

    I spent a lot of time because of the New Input System. I want to finish this problem. Is there anyone who can help you?

    1.PNG

    Code (CSharp):
    1.             #region aiming position
    2.             playerControls.PlayerMovement.Aiming.performed +=
    3.                 context =>
    4.                 {
    5.                     aimingInputs = context.ReadValue<Vector2>();
    6.  
    7.                     Vector3 worldPos = Vector3.zero;
    8.                     worldPos.x = aimingInputs.x;
    9.                     worldPos.z = aimingInputs.y;
    10.  
    11.                     Vector3 tempPos = Camera.main.ScreenToViewportPoint(worldPos);
    12.  
    13.                     Vector3 currentPos = transform.position;
    14.  
    15.                     Vector3 dirToTarget = worldPos - currentPos;
    16.  
    17.                     Quaternion targetRotation = Quaternion.LookRotation(dirToTarget);
    18.                     Quaternion playerRotation = Quaternion.Slerp(transform.rotation, targetRotation, 20f * Time.deltaTime);
    19.                     transform.rotation = playerRotation;
    20.                  };
    21.             #endregion
     
  2. 1; do not test these things in the editor, ever
    2; even if the mouse is out of the game view, rotation should be possible, why wouldn't be?

    Luckily you can't lock yourself out of the editor window and in the game view.
     
  3. devssun

    devssun

    Joined:
    Feb 1, 2015
    Posts:
    2
    The character rotates in the game view.
    But it's rotating according to the ratio based on my monitor size.

    Ex. Monitor Top Left to Top Right : Character Left to Right Rotation