How to set rotation of object by script?

like this?

gameObj.transform.eulerAngles.y = 180;

or

gameObj.transform.eulerAngles.y += 180;

4 Likes

In C# at least, I think you have to assign a value directly to the ‘eulerAngles’ field, e.g.:

gameObj.transform.eulerAngles = new Vector3(
    gameObj.transform.eulerAngles.x,
    gameObj.transform.eulerAngles.y + 180,
    gameObj.transform.eulerAngles.z
);

(I’m pretty sure this is right, but I can’t remember for sure at the moment.)

Also, your two examples are functionally different, so as far as choosing between them goes it just depends on what behavior you want.

23 Likes

All I had to do was really just you top answer, but instead of gameObj I put in a variable transform name. Like this:

pivot_Pan.transfor.eularAngles.y = 180;

1 Like

You can rotate transforms with Rotate
You can assign Quaternion rotations to transform.rotation
You can assign a Vector3 to the transform’s eulerAngles

11 Likes

And what if I have an object, rotated by X axis by N degrees (unknown non-zero value) and I need to set it rotation strictly to R value in? The target result is not rotation by N+R or N-R, remember that we don’t know N value, but we need to set rotation to known R.

1 Like

transform.Rotate is appropriate in that case.

transform.Rotate(R, 0, 0);
3 Likes

No, it is not. He (and me too) want to SET the value to R, not add R to it.

9 Likes

Based on what you have, you can choose either you want. The one with just “=” is fine, but the other one is 1 + 180. You get what I am saying?

none of this works! it gives me the error:

cannot modify the return value of transform.eulerAngles because it is not a variable

CODE:

mc.eulerAngles.y = 100;
//i already entered the code
public Transform mc;
//and verified it as Main Camera
1 Like

this guy said it right, it was the solution for me anyway

can anyone tell me how to rotate transform.rotate, with the up and down arrow key.

how to press a key an make the object rotate forward and backward in its local space.

The Unity API page for rotation shows you exactly that. :slight_smile:

I kept trying different methods and came up with this, it worked for me (Unity 2018.2.5f1):

transform.rotation = Quaternion.Euler(0, 0, 45);
21 Likes

If you want to set rotation as Editor :

transform.localRotation = Quaternion.Euler(1,1,1);
22 Likes

This is what I was looking for thanks a lot

2 Likes

I don’t know why but this is not working for me

i just want to change rotations on x and z to zero y must remain same. what do i put in y’s place?

That’s a harder question than you might expect, because Euler angles are pretty terrible in anything more complex than the most basic of applications. I have this longer writeup that I would highly recommend reading to clarify how.

You can plug in transform.rotation.eulerAngles.y to the second parameter of the above function, and it may work in some cases, but because the three Euler angles often all depend on each other in unpredictable ways, it’s not a good idea.

For setting Rotation or in other words, Set New Rotation to your transform.

transform.localRotation = Quaternion.Euler(0, 180, 0);  //Set Rotation value of y  to 180 and rest 0;
1 Like