I’m trying to make it so my player will change the direction they’re facing when the mouse is on the left or right side of the screen. I’m pretty new to unity and coding so I don’t know how to go about doing this. I also need to change the direction of the walk animation I’m going to be adding, because with what I’m doing I want the player to be able to walk backwards something but I’m not sure how to change the rotation of the whole animation in the direction the player is facing.
@Manninerd source: Make a Player Model Rotate towards Mouse Location - Questions & Answers - Unity Discussions
public class LookTowardMouse : MonoBehaviour {
// Update is called once per frame
void Update ()
{
//Get the Screen positions of the object
Vector2 positionOnScreen = Camera.main.WorldToViewportPoint (transform.position);
//Get the Screen position of the mouse
Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
//Get the angle between the points
float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
//Ta Daaa
transform.rotation = Quaternion.Euler (new Vector3(0f,0f,angle));
}
float AngleBetweenTwoPoints(Vector3 a, Vector3 b) {
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
}
}