Question by
AndrewM5 · May 08 at 03:08 AM ·
c#2d rotation
Changing 2D sprite rotation with min and max angles using mouse movement
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.
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
If anyone could help, that will 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
Comment