Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I rotate an object around its local X axis by A degrees?

Discussion in 'Scripting' started by mipo, Oct 16, 2014.

  1. mipo

    mipo

    Joined:
    Aug 22, 2013
    Posts:
    19
    I have a bunch of objects for which I want to control the local X rotation with a slider (0 - 360). I've tried all sorts of Quaternion operations but I can't get the right combination to have the rotation happen in the object's local space, they always rotate in world space.

    transform.Rotate() does exactly that but it's a rotate-by function and that's fine in an Update loop but here it's in an OnValueChanged() event and I need to set my objects to an absolute value each on its own X-axis, a rotate-to around the local-X-axis function.

    Is there any Quaternion-brain out there that can point me in the right direction?
    (pun intended)

    I'm using 4.6 beta 20 if that matters here.
    Thnx.

    EDIT: Answered here:
    http://forum.unity3d.com/threads/ho...ocal-x-axis-by-a-degrees.274408/#post-1813264
    Thnx Cpt.
     
    Last edited: Oct 18, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Transform.eulerAngles.

    --Eric
     
    mipo and JoeStrout like this.
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    This should do the trick :)
    Code (CSharp):
    1. Vector3 eulers = this.transform.rotation.eulerAngles;
    2. this.transform.rotation = Quaternion.Euler(new Vector3(sliderX,eulers.y,eulers.z));
    3.  
    4.  
     
    mipo likes this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Or you can just use Transform.eulerAngles. ;) It does exactly the same thing except with less code. There's no reason to do "this.transform.rotation.eulerAngles" when you can just do "transform.eulerAngles", and instead of Quaternion.Euler() you can just set transform.eulerAngles.

    --Eric
     
    ZO5KmUG6R likes this.
  5. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Ah , right. Forgot about that :p

    Code (CSharp):
    1. transform.eulerAngles = new Vector3(sliderX,transform.eulerAngles.y,transform.eulerAngles.z);
    2.  
     
  6. mipo

    mipo

    Joined:
    Aug 22, 2013
    Posts:
    19
    Thanks Eric but I tried that before coming here, and transform.localEulerAngles. Also I have tried all combinations of converting Quaternions to Euler and vice versa to no avail. All for some reason result in a rotation around the world axis. The only thing that seems to do the job for me is transform.Rotate which does not work with sliders.

    I guess I'll have to use two buttons instead. But I'd love to understand why the obvious
    transform.eulerAngles does not work here.

    CORRECTION: With transform.eulerAngles the objects do revolve around their own origin but they align with the world axis. With Rotate() they rotate correctly and align correctly....
     
    Last edited: Oct 17, 2014
  7. mipo

    mipo

    Joined:
    Aug 22, 2013
    Posts:
    19
    Thanks for the answer but it does not work. See my reply to Eric's first answer above.
     
  8. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    this seems like it would work i don't know if you have tried it yet or not
    Code (csharp):
    1. Quaternion localRotation = Quaternion.Euler(sliderDeltaX, 0f, 0f);
    2. transform.rotation = transform.rotation * localRotation;
    i typed sliderDeltaX because it would only work if you create the new localRotation based on how the slider has changed since the last frame, not the actual value of the slider.
     
    mipo likes this.
  9. mipo

    mipo

    Joined:
    Aug 22, 2013
    Posts:
    19
    No, haven't tried it. I have thought about the concept but couldn't figure out how to get that delta between frames. I was losing patience so I used toggles and Rotate() instead. I will try it on my next project. This is what I made : http://michelpoisson.net/unity/Mandala.html
     
  10. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    it usually only takes an extra variable and another line of code
    Code (csharp):
    1. private float sliderLastX = 0f;
    2.  
    3. void Update() {
    4.     Quaternion localRotation = Quaternion.Euler(sliderX - sliderLastX, 0f, 0f);
    5.     transform.rotation = transform.rotation * localRotation;
    6.     sliderLastX = sliderX;
    7. }
     
    hotchick, coloal, DiGatsby and 3 others like this.
  11. mipo

    mipo

    Joined:
    Aug 22, 2013
    Posts:
    19
    Yes, that did the job!
    Keeping track of the slider's last position was the key.
    Will definitely go into my next project.
    Thanks Cpt.
     
  12. Hootlook

    Hootlook

    Joined:
    Oct 27, 2018
    Posts:
    15
    Omg Cpt Chuckles...

     
  13. coloal

    coloal

    Joined:
    Sep 19, 2019
    Posts:
    3
    Bro, I love you right know. I have been looking for this for days. I suspected keeping track of this was the key but couldnt make it work. Thanks a lot!