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

Flying Controller - Troubles with Quaternion.Slerp

Discussion in 'Physics' started by BuhGaming, Jan 3, 2016.

  1. BuhGaming

    BuhGaming

    Joined:
    Dec 6, 2015
    Posts:
    7
    Here's the type of flying i'm trying to create:



    The character's direction follows the camera when i hold down the right mouse button, like in WOW.

    But when the mouse button is not held down, i need the camera to move independently of the characters movements.

    The first part works fine, the code looks like this:
    Code (CSharp):
    1.         if ( (Input.GetMouseButton (1))) {
    2.  
    3.             Quaternion cameraDirection = Camera.main.transform.rotation;
    4.  
    5.             //add in the tilt we should have from turning, this formula seems to work, but i don't understand why...
    6.             cameraDirection *= Quaternion.Euler(Vector3.forward * desiredTilt);
    7.  
    8.             //how much do we need to change to match the camera's rotation?
    9.             float diff = Quaternion.Angle(transform.rotation, cameraDirection);
    10.  
    11.             //Debug.Log("diff : "+ diff);
    12.  
    13.             transform.rotation = Quaternion.Slerp(transform.rotation, cameraDirection, (diff/3) * Time.deltaTime);
    14.  
    15.  
    16.         //if we're not holding down a mouse button to steer
    17.         }
    But i'm stuck on the second part. WASD should control the character when its not following the camera, like in WOW, but i need to rotate the z axis to correct for any remaining tilt in the dragon when i switch from following the camera to not following the camera, in this mode the code right now looks like this:

    Code (CSharp):
    1.         else
    2.         {
    3.  
    4.             rotationAmountHorizontal = -Input.GetAxis("Horizontal") * turnSpeed;
    5.  
    6.             target.transform.Rotate(-target.transform.up, rotationAmountHorizontal);
    7.  
    8. }
    I've tried all sorts of stuff to get the character to shift back to upright when i switch to WASD. Including using the above working code but changing the Camera.main.trasform.rotation references to transform.rotation, it seems to me that it should definitely work, but amazingly it doesn't. This doesn't work at all, and the only difference is that i'm copying my own rotation rather than the camera's rotation:

    Code (CSharp):
    1.             Quaternion cameraDirection = transform.rotation;
    2.  
    3.             //add in the tilt we should have from turning, this formula seems to work
    4.             cameraDirection *= Quaternion.Euler(Vector3.forward * desiredTilt);
    5.  
    6.             //how much do we need to change to match the camera's rotation?
    7.             float diff = Quaternion.Angle(transform.rotation, cameraDirection);
    8.  
    9.             transform.rotation = Quaternion.Slerp(transform.rotation, cameraDirection, (diff / 3) * Time.deltaTime);
    I'm stumped. I've tried a bunch of different techniques. Amazing how hard it is to just get the character to lerp to facing upright.