Search Unity

Question Changing 2D sprite rotation with min and max angles using mouse movement

Discussion in 'Scripting' started by AndrewM5, May 8, 2022.

  1. AndrewM5

    AndrewM5

    Joined:
    Jul 3, 2019
    Posts:
    8
    I am new to Unity so I have been mostly going by youtube videos and forums to help me along the way. I am using Unity version 2020.3.29f1.

    I am trying to make a 2D sidescroller where the player sprite changes direction whenever the mouse moves past a certain angle. I have gotten my player to change direction but I don't know how to add the min and max angles.

    This is my current code that changes my sprite to rotate on the z-axis. I found this on another form and tweaked it to what I needed. I would ideally like it to rotate along the y-axis instead of the z-axis, however, I wasn't able to do it with this code.

    Code (CSharp):
    1. Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
    2.         Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
    3.         lookPos = lookPos - transform.position;
    4.         float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
    5.         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    If anyone could help that was be appreciated.
    I am not opposed to switching this out for better code if it's easier, this is just the one I found that sort of worked
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Atan2() has an output range from -pi to +pi (-180 deg to 180deg)

    If you structure it right, you can get the one Atan2() call to return you the angle around you, with 0 being straight up, and then you could clamp it to -150 to +150 or whatever.

    And you can compare it to other lower numbers to decide when to flip left/right.
     
  3. AndrewM5

    AndrewM5

    Joined:
    Jul 3, 2019
    Posts:
    8
    I kinda of understand what you are saying. Since I have little knowledge of c# and Unity would you mind showing the code or giving an example of what I would need to change?
     
  4. AndrewM5

    AndrewM5

    Joined:
    Jul 3, 2019
    Posts:
    8
    After some messing around I found out that the original angle was not getting updated as I was moving my mouse around. I was passing in a Vector3 (ScreenToWorldPoint) as parameters x,y but it was never changing. I made them local variables and the angle now works with side effects. My player (parent) and bow (child) now rotates together at the same time rather than just the child. It also still rotates in the z-direction instead of y, and there are no min and max angles yet. That's what I was working toward.

    Code (CSharp):
    1. Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
    2.         Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
    3.  
    4.         float x = lookPos.x;
    5.         float y = lookPos.y;
    6.  
    7.         lookPos = lookPos - transform.position;
    8.         float angle = Mathf.Atan2(x, y) * Mathf.Rad2Deg;
    9.         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    10.  
    11.         Debug.Log("X:" + x.ToString()
    12.             + "  Y:" + y.ToString()
    13.             + "  Z:" + lookPos.z.ToString()
    14.             + "  A:" + angle.ToString()
    15.             );