[Answered]Help with Transform.Rotate()

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?

Hi, welcome to the forum!

To set the angle of rotation, you should create the rotation as a Quaternion and assign it to the transform.rotation property.

I am having the same problem.

I can’t seem to understand how to rotate the Quaternion works. how would I say set it to 30 degrees?

I guess a follow up question is when you pass it arguments does it interpret them as degrees, radians, or what?

Any help with this would be greatly appreciated.

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.

Thank you that solved my problem.

case SPREAD:

     shot = Instantiate( bullet, transform.position, transform.rotation ) as GameObject;

     GameObject shot2 = Instantiate( bullet, transform.position, (Quaternion.Euler(0, 10, 0)) ) as GameObject;
			
     GameObject shot3 = Instantiate( bullet, transform.position, (Quaternion.Euler(0, -10, 0)) ) as GameObject;
break;

As a note that is done in C#.

That answered my question. Thanks!