Search Unity

Resolved Character wobbling when rotating it to keep it on the inside of a ringworld

Discussion in 'Scripting' started by Mordus, Feb 13, 2021.

  1. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    I'm making a map that's curved back into a loop, like a ringworld/dyson ring.
    The character needs to have its up direction adjusted so that it's always towards the centre of the ring as the character moves around, but I'm having trouble with the rotations.

    In the update I need to make two changes to the characters rotation
    1 - to rotate it around its current up direction based on mouse input (so the player can turn).
    2 - to keep its up direction towards the center of the ring.

    I can do either of those things individually and it works fine. But if I do both the camera starts wobbling in a strange way when the player tries to turn around. The players look direction is basically restricted to a 180 degree arc and if you turn passed that it wobbles you back.

    Short clip of what's happening to demonstrate, since it's easier to show than tell.
    https://imgur.com/kp6u0PC

    Here is the code that's doing the rotation. This is using the "kinematic character controller" asset as the unity character controller doesn't support having arbitrary up directions.

    Code (CSharp):
    1.  
    2. public void UpdateRotation(ref Quaternion currentRotation, float deltaTime)
    3. {
    4.     // get the direction to the centre of the ring, to use as player up
    5.     var playerPosInRing = motor.TransientPosition;
    6.     playerPosInRing.x = 0f;
    7.     var toRingCentre = ((Vector3)c.ringCentre - playerPosInRing).normalized;
    8.  
    9.     var r = currentRotation;
    10.     // turn the character around its up axis based on mouse movement
    11.     r *= Quaternion.AngleAxis(inputs.mouseDelta.x * lookSensitivity, r * Vector3.up);
    12.     // rotate the character to make sure it's up direction is to the centre of the ring
    13.     r *= Quaternion.FromToRotation(r * Vector3.up, toRingCentre);
    14.     currentRotation = r;
    15. }
    16.  
    If i comment out the line that does mouse-movement-rotation, then the ring-up-rotation line works perfectly and i can walk all the way around the ring without issue.
    If i comment out the line that does the ring-up-rotation then the mouse movement rotation works perfectly.
    But when i do them both it glitches like you can see in that imgur clip.
     
    Last edited: Feb 13, 2021
  2. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    Nevermind. I was doing the quaternion multiplication the wrong way around. Just reverse the left/right hand sides of the multiplications and it works.