Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Upper and lower body rotation issue

Discussion in 'Scripting' started by YuukanOO, Dec 1, 2023.

  1. YuukanOO


    Jan 7, 2013
    Hi there, I've got a problem related to rotation that's driving me crazy.

    I'm pretty new in the 3D world and I'm playing with character movement. The goal is to:
    • Have the upper body face the player's looking direction (determined by the mouse position related to the character, this part is working as expected)
    • Have the lower body face the same direction as the looking one based on the player move direction and use a lerp to make the rotation smooth
    The issue arise when the lower body target direction is the exact inverse of the player movement (because the lower body should go backwards, angle of 180°). For example, the user is looking in the upper right and moving straight left so its legs should face straight right because he's going backwards. If he's moving right and facing right, then the lower direction is right too because he's going forwards.

    As you can see in the attached video, sometimes (when the angle needed is 180°), the lower body rotates in the wrong direction and this look weird (when I'm looking up in the video below).


    Colors are:
    • Yellow: user's looking direction
    • Green: current user move direction
    • Blue: current character forward direction
    • Red: target lower body direction
    The simplified Update code looks like this:

    Code (CSharp):
    1. void Update()
    2.   {
    3.     // (...)
    5.     // _look is the direction the user mouse is facing (yellow)
    6.     // _move is the current direction the user is moving (green)
    7.     // _lowerDirection is the final direction the character lower body should face (red, based on the user looking direction and its movement)
    8.     // _characterMesh.transform.forward is the current direction the character is facing (blue)
    10.     _characterMesh.transform.rotation = Quaternion.Lerp(
    11.       _characterMesh.transform.rotation,
    12.       Quaternion.LookRotation(_lowerDirection), Time.deltaTime * TURN_SPEED);
    14.     // (...)
    15.   }
    I've already tried a lot of things but struggle to find a good way to achieve this, could someone help me? :)