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 can I change an object's rotation?

Discussion in 'Scripting' started by Callski, Feb 3, 2013.

  1. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130
    Code (csharp):
    1.  
    2.  object.transform.Rotate(180, 0 ,0)
    3.  

    When I use the above code instead of changing my object'x X rotation field to 180 degrees the rotation field changes to
    -5, 180, 180. How can I change my object's rotation through a script to specific degrees like 180,0 ,0?
     
  2. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    is the object a child or another object? Maybe set space to world space instead of self space (local).

    Also, are your initial rotation (0, 0, 0). It doesn't set the rotation to 180 but rotates it 180 degrees, so if your at 20 it will go to 200.
     
  3. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130
    It is initially at (0,0,0). No its not a child of another object. I just tried

    Code (csharp):
    1.  
    2.  backgroundPlane.transform.Rotate(1,0,0);
    3.  
    That set the object's X rotation to 0.9999999.
    If I set the X rotation to 20 the object's X rotation is perfectly 20. I guess I'll just have to play around with it and see.
    I also tried rotating it 90 degrees twice. The first time X was 90 y was 0 and z was zero. The second time however
    made X 0, Y 180, and Z 180 which doesn't really make sense to me. I'm sure I just need to spend a little more time and figure it out. If I do of course I'll post my code here.
     
  4. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130

    I don't know what my problem was but everything is now working great. I use:
    Code (csharp):
    1.  
    2.  backgroundPlane.transform.Rotate(-90,0,0);
    3.