Sprite flip fighting with Lerp

Hey,

I’m having an issue with the player being flipped by the PlayerController fighting against the lerp to rotate the Character Sprite.

It seems like the Lerp takes into account the world position when calculating its target position. Therefore, if the currentCharacterRotation.y is 0 then the targetCharacterRotation wants to be NET 0, but the flipped object is at -180. Therefore, the targetCharacterRotation was set to 180 (as 180-180 is 0).

At this point my head exploded.

I changed the code from:

        Quaternion currentCharacterRotation = _characterSpriteTransform.rotation;
        Quaternion targetCharacterRotation = Quaternion.Euler(currentCharacterRotation.x, currentCharacterRotation.y, targetAngle);

        _characterSpriteTransform.rotation = Quaternion.Lerp(currentCharacterRotation, targetCharacterRotation, _tiltSpeed * Time.deltaTime);

to

        Quaternion currentCharacterRotation = _characterSpriteTransform.localRotation;
        Quaternion targetCharacterRotation = Quaternion.Euler(currentCharacterRotation.x, currentCharacterRotation.y, targetAngle);

        _characterSpriteTransform.localRotation = Quaternion.Lerp(currentCharacterRotation, targetCharacterRotation, _tiltSpeed * Time.deltaTime);

And that seemed to work.

My other little issue was that the lean flipped as well. So moving forward to the right had the player leaning forward, but moving forward to the left had the player leaning back. So I created a private bool, _isFlipped in PlayerController and set it in the HandleSpriteFlip() function. I exposed a public bool, IsFlipped by returning _isFlipped.

    public bool IsFlipped => _isFlipped;
    private bool _isFlipped;

This I used in PlayerAnimation to set a positive or negative _tiltAngle in ApplyTilt

        if (PlayerController.Instance.MoveInput.x < 0.0f)
        {
            if (PlayerController.Instance.IsFlipped)
            {
                targetAngle = -_tiltAngle;
            }
            else
            {
                targetAngle = _tiltAngle;
            }
        }
        else if (PlayerController.Instance.MoveInput.x > 0.0f)
        {
            if (PlayerController.Instance.IsFlipped)
            {
                targetAngle = _tiltAngle;
            }
            else
            {
                targetAngle = -_tiltAngle;
            }
        }
        else
        {
            targetAngle = 0.0f;
        }
1 Like

Privacy & Terms