Search Unity

a little help with quaternions

Discussion in 'Editor & General Support' started by Innovine, Sep 24, 2020.

  1. Innovine

    Innovine

    Joined:
    Aug 6, 2017
    Posts:
    522
    I could really do with some help here as I have tried everything I can think of and still cant get the results I am after, and I figure its not too complicated...

    Imagine a spherical planet, with a spaceship parked on its surface. The spaceship has pitch, roll, and yaw gauges. These are trivial to calculate for world space, but I want to add a mode where it is calculated relative to the surface. That means that yaw is measured around the local up, with north being a yaw of 0, and pitch as the angle above or below the local horizon.

    How can I calculate these pitch roll and yaw values relative to the surface?

    This is what I have tried so far, but it's not quite working...
    Code (CSharp):
    1.      
    2. Vector3 planetToShip = (ship.transform.position - Planet.transform.position).normalized;
    3. Vector3 north = Vector3.up;
    4. Vector3 crossProduct = Vector3.Cross(planetToShip, north);
    5. Vector3 viewNorth = Vector3.Cross(crossProduct, planetToShip);
    6.  
    7. Quaternion rot = Quaternion.LookRotation(viewNorth, planetToShip);
    8.  
    9. ShowRotation(rot * Quaternion.Inverse(ship.transform.rotation));
    10.  
    ShowRotation() just displays the pitch, roll and yaw values. I have tried various operations between rot and the ship rotation and just cant get the right results out of it. Please help!!
     
    Last edited: Sep 24, 2020