Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to set rotation of object by script?

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

Thread Status:
Not open for further replies.
  1. leegod

    leegod

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

    gameObj.transform.eulerAngles.y = 180;

    or

    gameObj.transform.eulerAngles.y += 180;
     
    helgrind21 and Lee200 like this.
  2. Jesse Anders

    Jesse Anders

    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

    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

    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

    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

    StarManta

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

    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

    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

    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

    NinjaProgrammer007

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

    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

    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

    Doug_B

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

    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

    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);
     
  16. saadsaiyed7

    saadsaiyed7

    Joined:
    Apr 9, 2019
    Posts:
    2
    This is what I was looking for thanks a lot
     
    badfatcat and GreNEvIL14 like this.
  17. saadsaiyed7

    saadsaiyed7

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

    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

    StarManta

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

    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.
  21. adrianzarp

    adrianzarp

    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

    rozsazsombor70228

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

    Avivyouker

    Joined:
    Aug 5, 2020
    Posts:
    14
    Thanks you helped me in my project :D
     
  24. bluebean067

    bluebean067

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

    elfnik

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

    void Update()
    {
    transform.rotation = Quaternion.identity;
    }
     
  26. InUnit-y

    InUnit-y

    Joined:
    Nov 9, 2020
    Posts:
    1
     
  27. roko123

    roko123

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

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    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

    chainswordkiller1

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

    MelvMay

    Unity Technologies

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

    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

    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;
     
  33. iydrss

    iydrss

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

    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

    Eatnine

    Joined:
    Feb 14, 2022
    Posts:
    2
    transform.rotation = Quaternion.Euler(0,0,0);
     
    Kenan_Nynor likes this.
Thread Status:
Not open for further replies.