I’m making a top-down 2D game. But I can’t make my player object to rotate to the direction it’s moving.
void Update() {
float HorizontalMove = Input.GetAxis ("Horizontal");
float VerticalMove = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (HorizontalMove, 0.0f, VerticalMove);
float newAngle = Vector3.Angle (transform.forward, movement);
transform.rotation = Quaternion.AngleAxis (newAngle, Vector3.forward);
}
This code makes my object face the direction I want, however, this works only for the left direction. When I move right, it still rotates to the left.
Also, I want the script to remember the last angle and add it up to the next one, so that the object always faces where it goes.
Sorry for my bad English. Maybe it’ll become better someday.