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

Handle Character Movement Details

Discussion in 'Physics' started by asored, May 3, 2021.

  1. asored

    asored

    Joined:
    Nov 6, 2020
    Posts:
    22
    Hi guys,

    for my Unity game I have a lion as character. First of all I try to handle a movement logic like that:



    As Character Controller I'm using the Kinematic Character Controller from asset store. The basic behavior in the example is that the character looks always forward. But I need the following:

    1) The character should rotate to the direction he moves. Otherwise I have unnatural movement behavior for my purpose

    2) This would be optional but amazing: the character should always rotate parallel to the ground, for example when he goes up to a mountain. With my tries this works only with hard camera flickering.

    I've recorded a video to show you what I have and what's my intention:



    The rotation logic is defined in a `UpdateRotation` method that currently looks like this:


    public void UpdateRotation(ref Quaternion currentRotation, float deltaTime)
    {
    if (_lookInputVector != Vector3.zero && OrientationSharpness > 0f)
    {
    // Smoothly interpolate from current to target look direction
    Vector3 smoothedLookInputDirection = Vector3.Slerp(Motor.CharacterForward, _lookInputVector, 1 - Mathf.Exp(-OrientationSharpness * deltaTime)).normalized;
    currentRotation = Quaternion.LookRotation(smoothedLookInputDirection, Motor.CharacterUp);
    }
    }


    This is my try to create a movement I need, but yes.....you can see the bugs in the video:


    public void UpdateRotation(ref Quaternion currentRotation, float deltaTime)
    {
    if (_lookInputVector != Vector3.zero && OrientationSharpness > 0f)
    {
    // Smoothly interpolate from current to target look direction
    Vector3 smoothedLookInputDirection = Vector3.Slerp(Motor.CharacterForward, _lookInputVector, 1 - Mathf.Exp(-OrientationSharpness * deltaTime)).normalized;

    // Set the current rotation (which will be used by the KinematicCharacterMotor)

    // Custom: Move Player correctly
    float horizontal = Input.GetAxisRaw("Horizontal");
    float vertical = Input.GetAxisRaw("Vertical");

    _lastDirection = new Vector3(horizontal, 0.0f, vertical).normalized;

    Debug.Log(_lastDirection);


    float targetAngle = Mathf.Atan2(_lastDirection.x, _lastDirection.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, 0.1f);
    transform.rotation = Quaternion.Euler(0f, angle, 0f);
    currentRotation = Quaternion.Euler(0f, angle, 0f);
    currentRotation = Quaternion.LookRotation(smoothedLookInputDirection, Motor.CharacterUp);
    }
    }


    Thanks in advance for your assistance. I really appreciate it!

    PS: I hope my code gets formatted better when I publish this post as in the preview :)
    PPS: Nope....

    Best regards,
    Asored