# How to set rotation of object by script?

Discussion in 'Scripting' started by leegod, Jul 23, 2010.

Not open for further replies.
1. ### leegod

Joined:
May 5, 2010
Posts:
2,493
like this?

gameObj.transform.eulerAngles.y = 180;

or

gameObj.transform.eulerAngles.y += 180;

2. ### j_y_k

Joined:
Apr 5, 2008
Posts:
2,857
In C# at least, I think you have to assign a value directly to the 'eulerAngles' field, e.g.:

Code (csharp):
1. gameObj.transform.eulerAngles = new Vector3(
2.     gameObj.transform.eulerAngles.x,
3.     gameObj.transform.eulerAngles.y + 180,
4.     gameObj.transform.eulerAngles.z
5. );
(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.

3. ### Major

Joined:
Jul 26, 2012
Posts:
69
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;

Tonymotion likes this.
4. ### Loius

Joined:
Aug 16, 2012
Posts:
546
You can rotate transforms with Rotate
You can assign Quaternion rotations to transform.rotation
You can assign a Vector3 to the transform's eulerAngles

5. ### Aldeminor

Joined:
May 3, 2014
Posts:
18
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.

CosmonautGames likes this.
6. ### StarManta

Joined:
Oct 23, 2006
Posts:
8,777
transform.Rotate is appropriate in that case.

Code (csharp):
1.
2. transform.Rotate(R, 0, 0);
3.

V5Studio, Fantinha and teheKraken like this.
7. ### quizcanners

Joined:
Feb 6, 2015
Posts:
109
No, it is not. He (and me too) want to SET the value to R, not add R to it.

8. ### McCree

Joined:
Nov 18, 2015
Posts:
2
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?

9. ### NaveGCT

Joined:
Dec 20, 2017
Posts:
5
none of this works! it gives me the error:
Code (CSharp):
1. cannot modify the return value of transform.eulerAngles because it is not a variable
CODE:
Code (CSharp):
1. mc.eulerAngles.y = 100;
2. //i already entered the code
3. public Transform mc;
4. //and verified it as Main Camera
5.

axlvc likes this.
10. ### NinjaProgrammer007

Joined:
Mar 24, 2017
Posts:
2
this guy said it right, it was the solution for me anyway

11. ### Anhella

Joined:
Nov 18, 2013
Posts:
67
can anyone tell me how to rotate transform.rotate, with the up and down arrow key.

12. ### Anhella

Joined:
Nov 18, 2013
Posts:
67
how to press a key an make the object rotate forward and backward in its local space.

13. ### Doug_B

Joined:
Jun 4, 2017
Posts:
1,596
The Unity API page for rotation shows you exactly that.

14. ### ItsAtomTime

Joined:
May 5, 2018
Posts:
1
I kept trying different methods and came up with this, it worked for me (Unity 2018.2.5f1):
Code (CSharp):
1. transform.rotation = Quaternion.Euler(0, 0, 45);

15. ### snowinrain

Joined:
Jan 17, 2016
Posts:
15
If you want to set rotation as Editor :
Code (CSharp):
1. transform.localRotation = Quaternion.Euler(1,1,1);

Joined:
Apr 9, 2019
Posts:
2
This is what I was looking for thanks a lot

badfatcat and GreNEvIL14 like this.

Joined:
Apr 9, 2019
Posts:
2
I don't know why but this is not working for me

18. ### agha502

Joined:
Dec 21, 2018
Posts:
3
i just want to change rotations on x and z to zero y must remain same. what do i put in y's place?

19. ### StarManta

Joined:
Oct 23, 2006
Posts:
8,777
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.

20. ### Deirvlon

Joined:
Oct 14, 2013
Posts:
4
For setting Rotation or in other words, Set New Rotation to your transform.

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

Weiser-Industries likes this.

Joined:
Feb 25, 2014
Posts:
4
You could also do it like this:

transform.eulerAngles = new Vector3(0, 180, 0);

pato3848 and sore1899_ like this.
22. ### rozsazsombor70228

Joined:
Mar 18, 2020
Posts:
2
this worked for me too, thanks

23. ### Avivyouker

Joined:
Aug 5, 2020
Posts:
14
Thanks you helped me in my project

24. ### bluebean067

Joined:
Feb 3, 2020
Posts:
1
What I did was
1. transform.rotation.eulerAngles.Set(0, 0, 0);

25. ### elfnik

Joined:
Feb 3, 2018
Posts:
8
Use this simple script in update method:

void Update()
{
transform.rotation = Quaternion.identity;
}

Joined:
Nov 9, 2020
Posts:
1

27. ### roko123

Joined:
Jan 8, 2021
Posts:
1
How do you make the x value stay the same and not change

28. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
39,409
Please don't reply to 11-year-old posts. Start your own new post, it's FREE!

When you post, here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

29. ### chainswordkiller1

Joined:
May 25, 2021
Posts:
1
asshole this is continously rotate the object

### Unity Technologies

Joined:
May 24, 2013
Posts:
11,696
Language like that has no place on these forums. If you cannot keep it civil, please take it elsewhere.

31. ### dalitbot123

Joined:
Apr 17, 2021
Posts:
1
why does it say this error in the inspector:
Cannot modify the return value of 'Transform.eulerAngles' because it is not a variable

32. ### Weiser-Industries

Joined:
Nov 24, 2020
Posts:
1
do this one
1. transform.localRotation = Quaternion.Euler(0, 180, 0); //Set Rotation value of y to 180 and rest 0;

Joined:
Apr 22, 2020
Posts:
4
working
thanks..

34. ### ivanpost777

Joined:
Sep 4, 2020
Posts:
1
Code (csharp):
1. gameObj.transform.eulerAngles = new Vector3(
2.     gameObj.transform.eulerAngles.x,
3.     gameObj.transform.eulerAngles.y + 180,
4.     gameObj.transform.eulerAngles.z
5. );
alternatively, a thing like this:
Code (csharp):
1. gameObject.transform.rotation = Quaternion.Euler(targetRotationX, targetRotationY, targetRotationZ);
may work as well.

Quaternion is declared as struct in Unity engine - this is a reason of message "Cannot modify the return value of 'Transform.eulerAngles' because it is not a variable", structs act differently in C#

35. ### Eatnine

Joined:
Feb 14, 2022
Posts:
2
transform.rotation = Quaternion.Euler(0,0,0);

Kenan_Nynor likes this.