Hi. I can’t find/figure how to make it so that when I press a directional key (or two at once), my Game Object/character controller goes in the corresponding direction. Basically, when the user presses Left, the object will face that direction in move. Right now, I use Transform.Rotate, but it only adds to the object’s current angle, instead of “setting” it at that angle like it should.
Observe:
if (Input.GetKey("down") direction != 2)
{
transform.Rotate(0, 180, 0);
direction = 2;
}
if (Input.GetKey("up") direction != 1)
{
transform.Rotate(0, 0, 0);
direction = 1;
}
if (Input.GetKey("left") direction != 3)
{
transform.Rotate(0, 270, 0);
direction = 3;
}
if (Input.GetKey("right") direction != 4)
{
transform.Rotate(0, 90, 0);
direction = 4;
}
How do I make it rotate towards a direction and not just rotate by that angle?
You can rotate around the world axes using Quaternion.Euler. The angles are given in degrees - everything in Unity works in degrees except the trigonometric functions, which use radians.