how do i get my player to rotate towards quaternion.identity? also how to fix walking up slopes

im making a 2d platformer game and currently, when the player sits on a slope, they face the direction the slope is. this means they are tilted. however, when you jump, it looks very awkward to jump at a 45 degree angle so i added this line of code

if(!isGrounded)
        {
            transform.rotation = Quaternion.identity;
        }

the problem with this is that it is very sudden so now i am wondering, how can i get the player to rotate to quaternoin.identity smoother and not have an instant transition?

also the problem with this is that if the slope is too steep, it wont register as being on the ground so it will rotate to quaternion.identity but then, the groundcheck will think that it is grounded and thus will be able to walk up slopes that they should not be able to. how can i ffix this?

You can use the static lerp methods of Quaternion like Slerp or Lerp.

float lerpWeight = 0.1; // Any value between 0 and 1, 0 being slow and 1 being sudden. Time.deltaTime should also work.
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, lerpWeight);

How exactly do you determine if the player is on the ground?

i use physics2d.overlapcircle

Can you post a picture of how that slope thingy happens? A video would be better.

here you go, i downloaded screencast o matic for this xDDDD
sry for rly loud audio
https://drive.google.com/drive/folders/1SUVYi4fbe3JMm028bjHS5yBN6By-Mttq?usp=sharing

i couldnt add the mp4 directly to this so i put it in google drive folder

So you basically don’t want the player to walk to slopes that are too steep.

A. You can follow Sebastian Lague’s tutorial on platformers:
Playlist:

Video where the slope was tackled:
https://www.youtube.com/watch?v=cwcC2tIKObU

B. Get the dot product of the normal of the platform you hit and the Vector2/3.up and determine if that is a valid slope from that. I recommend using CircleCast instead to get the normal of the collision.
Vector2.Dot: Unity - Scripting API: Vector2.Dot
CircleCast: Unity - Scripting API: Physics2D.CircleCast

var hit = Physics2D.CircleCast(origin, radius, direction); // Note:
// In my experience the "direction" argument
// is not really important so you can leave that as Vector2.zero

float dot = Vector2.Dot(hit.normal, Vector2.up);
float absDot = Mathf.Abs(dot); // Get the abs since dot product is -1 to 1
const float limit = 0.5f;
if (absDot > limit)
{
    // Stop or slow down the player
}

how would i nessecarily stop the player? i dont want to add a force in the opposite direction cuz that might be buggy and seems kind of weird

also i did this to my code

var hit = Physics2D.CircleCast(groundCheckPos.position, groundRadius, Vector2.zero);
        // Note:
        // In my experience the "direction" argument
        // is not really important so you can leave that as Vector2.zero

        float dot = Vector2.Dot(hit.normal, Vector2.up);
        float absDot = Mathf.Abs(dot); // Get the abs since dot product is -1 to 1
        const float limit = 0.5f;
        if (absDot > limit)
        {
            // Stop or slow down the player
            canWalk = false;
        }
        else
        {
            canWalk = true;
        }
private void FixedUpdate()
    {
        moveInput = Input.GetAxis("Horizontal");
        if (canWalk)
            rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
        else
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y);
    }

and now i cant walk

i debugged the value of dot and it was constantly -1

Can you debug the value of hit.normal?

when in mid air, normal is (0, 0)
when on flat ground normal is (0, 1)
when on 45 degree slope, normal is (0.7, 0.7)

7495949--923092--upload_2021-9-14_15-24-47.png

This is an interesting question. However, practically speaking, the vast majority of games do not tilt players which are in contact with a slope! It is far more natural to just fix the player’s rotation. If you think to your favourite 2D platformers, chances are that the characters walk on slopes as if it were flat ground. At most they might slow you down, or make you slip down due to gravity.

o, thanks, so should i just freeze rotation on rb?

You probably don’t need to freeze it, just remove the logic where you rotate the player on a slope. I know it probably feels bad to just remove something you’ve worked on for a long time, but ultimately this is a design problem.

i dont have any code to rotate it xDDD its just a side affect from my script, i change the rb velocity and it just happens to rotate it onto the slope lol

now thi s just looks weird

It looks weird because the sprite is literally a square. Replace it with a character sprite and it will look normal.

oh, ok