GameObject slide rotation not working.

Hi! I have a script that should rotate a player when ‘PlayerState == “Sliding”’. However, it clips me into the ground. I would like to have it rotate in a fluid animation over a short period of time, say, .2s, and then when ‘PlayerState /= “Sliding”’ rotate back to normal. How would I implement this? I have looked online, but rotation in Unity is kind of weird, and I can’t seem to get it to work right. Thanks!

  • npt
    Code:
        if(playerState == "sliding")
        {
            //slideRotationCurrent = Mathf.Lerp(transform.rotation.z, slideRotation, slideRotateTime * Time.deltaTime);
            slideRotationCurrent = slideRotation;
            Debug.Log("Changing Rotation - Sliding");
        } else {
            //slideRotationCurrent = Mathf.Lerp(transform.rotation.z, 0, slideRotateTime * Time.deltaTime);
            slideRotationCurrent = 0;
        }
    transform.eulerAngles = new Vector3(0, 0, slideRotationCurrent);
   
    }

transform.rotation returns a Quaternion, so when you’re accessing the z value, it’s not the same as the eulerAngle value. When you want to access the eulerAngle z value, you want to get the transform.eulerAngles first, like you did in the last line.

So I tried making it to when you start sliding, it calls a coroutine that uses a lerp to rotate the player to the slideRotation - 90 degrees over the period of time in slideDuration. Then, when you stop sliding, it rotates you back over the amount of time in slideRotation divided by how far in the animation you were - ie. if you were halfway to fully rotated when you started sliding, then it would take half the time to get back. However, it throws a ton of errors, and as I haven’t really used coroutines, I am not sure where I went wrong.
Code:

    void SlideScaling()
    {
       
        if(playerState == "sliding")
        {
            StartCoroutine(SlideFunctionStart);
        } else {
            StartCoroutine(SlideFunctionEnd);
        }
    }
   
    IEnumerator SlideFunctionStart()
    {
        slideRotateTime = 0;
        float startValue = transform.eulerAngles.z;
        while(slideRotateTime<slideRotateDuration)
        {
            transform.rotation = Quaternion.Lerp(startValue, slideRotation, slideRotateTime / slideRotateDuration);
            slideRotateTime += Time.deltaTime;
            yield return null;
        }
        transform.rotation = 90;
    }
   
        IEnumerator SlideFunctionEnd()
    {
        slideRotateTime = slideRotateDuration(90/transform.eulerAngles.z);
        float startValue = transform.eulerAngles.z;
        while(slideRotateTime<slideRotateDuration)
        {
            transform.rotation = Quaternion.Lerp(startValue, 0, slideRotateTime / slideRotateDuration);
            slideRotateTime += Time.deltaTime;
            yield return null;
        }
        transform.rotation = 0;
    }

Errors:

Variables:
float slideRotation - How much the player should rotate in a slide
float slideRotateDuration - How long the slide animation should last.
float slideRotateTime - How long the slide animation has lasted.

Any help on how to fix the errors? Am I doing something completely wrong?
Thanks - npt

There’s a lot wrong here. The errors tell you pretty much everything you did wrong. Nothing wrong with how you implemented coroutines, it’s just basic code errors.