Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Apply direction between mouse pointer and screen center to player transform

Discussion in 'Scripting' started by mrCharli3, Sep 20, 2023.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    I want to rotate my player around the Y-axis, normally I would just get the world position of the pointer and look at it, but I am overlaying my entire screen with a render texture to get a pixel effect, so I need to use screen space.

    I can get the direction between my pointer and screen center, but I am struggling how to apply that direction to the player. i.e if pointer is at the north of the screen, the player should look north.

    What I have so far, which does rotate the player but not in the right direction:


    Code (CSharp):
    1. public Vector3 GetDirectionBetweenCenterAndMouse()
    2.         {
    3.             // Get the mouse position in screen coordinates
    4.             Vector3 mousePosition = Input.mousePosition;
    5.  
    6.             // Calculate the center of the screen
    7.             Vector3 screenCenter = new Vector3(Screen.width / 2f, Screen.height / 2f, 0f);
    8.  
    9.             // Calculate the direction vector from the center to the mouse
    10.             Vector3 direction = mousePosition - screenCenter;
    11.  
    12.             // Normalize the direction vector if needed
    13.             direction.Normalize();
    14.  
    15.             // Now 'direction' contains the direction from the center of the screen to the mouse
    16.             Debug.Log("Direction: " + direction);
    17.  
    18.             // Calculate the angle in radians
    19.             float angleRadians = Mathf.Atan2(direction.y, direction.x);
    20.  
    21.             // Convert the angle to degrees
    22.             float angleDegrees = angleRadians * Mathf.Rad2Deg;
    23.  
    24.             // Ensure the angle is between 0 and 360 degrees
    25.             if (angleDegrees < 0)
    26.             {
    27.                 angleDegrees += 360f;
    28.             }
    29.  
    30.             Debug.Log("Angle in degrees: " + angleDegrees);
    31.  
    32.             cube.transform.eulerAngles = new Vector3(0, angleDegrees, 0);
    33.  
    34.             return direction;
    35.         }
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    If something is rotating in the wrong direction then you can negate the rotation value like so:

    Code (CSharp):
    1. cube.transform.eulerAngles = new Vector3(0, -angleDegrees, 0);
     
    Last edited: Sep 20, 2023
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    Or try this:

    Code (CSharp):
    1.     void CursorLook()
    2.     {
    3.         Vector3 direction=Input.mousePosition-new Vector3(Screen.width/2,Screen.height/2,0);
    4.         cube.transform.LookAt(cube.transform.position+new Vector3(direction.x,0,direction.y));
    5.     }
     
  4. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    It’s more than just the opposite direction, the math is off somewhere
     
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    Okay instead of negating the rotation, try flipping the x direction on line 19.

    Like so:

    19. float angleRadians = Mathf.Atan2(direction.y, -direction.x);
     
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    And change line 22 to:

    22. float angleDegrees = (angleRadians * Mathf.Rad2Deg)-90;
     
  7. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Thanks I’ll give all of this a try once I get on the comp!
     
  8. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    This "Almost" works, but for some reason I get this weird offset, not sure what is causing it.

    upload_2023-9-21_13-40-40.gif

    EDIT: Weirdly I get the exact same offset issue with the other solution.
     
  9. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Solved! I had to account for my isometric angle :)

    Thank you!

    Although for some reason when I replace screen center with an object being rendered at the center of the camera, it stops working. But Ill dig into that tonight!

    Vector3 direction = Input.mousePosition - Camera.main.WorldToScreenPoint(GameobjectInCenterOfView);
     
    Last edited: Sep 21, 2023