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

I am having trouble rotating an object about a relative axis

Discussion in 'Scripting' started by Hexadron, Apr 21, 2014.

  1. Hexadron

    Hexadron

    Joined:
    Jun 5, 2013
    Posts:
    4
    There is an object with an arbitrary rotation. I want to rotate it about its local X, Y, or Z axis. By this definition, the object's right, up, or forward vectors would remain unchanged for each respective axis. When I try to do this with quaternions, it does not behave as expected:

    Code (csharp):
    1.  
    2. // For the X axis only:
    3. target.transform.rotation *= Quaternion.AngleAxis(1, target.transform.right);
    4.  
    When I do this, the object rotates in some seemingly random direction and the right vector changes. What am I doing wrong? It works fine when the object is in its initial rotation with euler angles <0, 0, 0>.

    Other potential solutions that I have tried involve using a LookRotation with a varying up vector, parenting the object in another object and modifying its local rotation, and screwing with raw euler angles.

    Thanks in advance.

    IMAGES: http://imgur.com/a/oWGXD
     
    Last edited: Apr 21, 2014
  2. davidsirmons

    davidsirmons

    Joined:
    Mar 16, 2014
    Posts:
    190
    Reading this, I have no idea what you're visually referring to or trying to achieve. Posting an image will help clarify for everyone.
     
  3. Hexadron

    Hexadron

    Joined:
    Jun 5, 2013
    Posts:
    4
  4. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,548
    Hi!

    The order in which you multiply Quaternions matters. If you want to rotate a rotation by something, that something needs to come first. So go with
    Code (csharp):
    1.  transform.rotation = Quaternion.AngleAxis(...) * transform.rotation;
    Cheers,
    Pärtel
     
  5. Hexadron

    Hexadron

    Joined:
    Jun 5, 2013
    Posts:
    4
    I now see why this is necessary, but the problem does not yet seem to be fixed. Would you mind taking another look?

    In my code, I instantiate a sphere every frame in the direction of the object's right vector. It does not stay the same, and I have illustrated its path in black in the image below.

    Code (csharp):
    1.  
    2. var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    3. sphere.transform.position = target.transform.position + target.transform.right;
    4.  
    5. target.transform.rotation = Quaternion.AngleAxis(1, target.transform.right) * target.transform.rotation;
    6.  
    $uCzPYmT.png
     
  6. Hexadron

    Hexadron

    Joined:
    Jun 5, 2013
    Posts:
    4
    I've also found another inconsistency in the expected behavior - when I use the LookAt method on the transform, it produces similar crap.

    Code (csharp):
    1.  
    2. var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    3. sphere.transform.position = point;
    4.  
    5. target.transform.LookAt(point);
    6.  
    In this case, the variable point is the position on an invisible plane intersecting the object, coplanar with the axes control pictured below.

    $By6pivP.png

    I'm sure that you would agree that it's not looking at any of those points...