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

Problem about Quaternions and rotations in one axis

Discussion in 'Scripting' started by Giustitia, Oct 16, 2019.

  1. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Hi everybody!

    So, here I come with typical problems: Quaternions! :D

    Let's try to explain my case. I have a Quaternion stored from the forward of a transform. After store it, I rotate the transform to another direction, so I have the current localRotation of the transform and the previous stored rotation. With that info, I need to get a Vector3 rotated using the current transfom Y and Z rotations, but keeping the X of the stored Quaternion. Basically, a vector pointing to my current transform.forward but keeping the X angle of the stored rotation.

    I've tried almost thousand of ways to get it using Quaternion.Euler() and eulerAngles conversions, but as always thats not a good idea due to the nature of the relation between Quaternions and euler angles (things happen like rotating the X past 90 or -90 makes Y and Z component become 180 when getting eulerAngles and this kind of bad things)

    At the moment, my approach is going this way:
    Code (CSharp):
    1. Gizmos.DrawLine(transform.position, transform.position + startRotation * Quaternion.Inverse(transform.localRotation) * transform.forward * 5);
    With that, the Line always points to the direction stored on startRotation, but what I need is having the Line rotating on Y and Z, and only keeping the X unrotated. I'll try to show you with this beautiful pic :)



    Left: What I get //////////// Right: What I need

    The main purpose of this mess, besides drawing this lines, is to get the angle difference in degrees between the two angles in X and Y separately.

    Thanks to everybody that spend more than 5 seconds of his life reading me :)
     
    Last edited: Oct 16, 2019