Getting Player GameObject to rotate to the slope of the ground it is walking on

Hi there,

I’m a beginner to Unity and I’ve created a simple 2D platformer project at the moment whereby I can control the Player horizontally. I’m just trying to figure out how to get the player to rotate to the slope of the ground similar to how Sonic The Hedgehog rotates as he runs up a hill or around a loop?

From reading online I know that getting the player to rotate with the slope of the ground has something to do with the slope’s “normal”? I understand that it is possible to figure out what the slope of the ground is by calculating the angle between “global up” and the slope’s “normal”. But how do I use this information to make the player rotate with the slope of the ground?

On top of this I know that it is possible to use the “Quaternion.LookRotation” function in order to get the Player to rotate with the slope of the ground but again I don’t really know how to go about doing this…

Kind regards

Use rigidbodys! Heres some code, NOTE: This freezes roation when in air so no wonky things happen.

RigidBody2D rb = GetComponent<RigidBody2D>();
if(IsGrounded)
{
      rb.freezeRotation = false; //unfreezes rotation when on ground
} else
{
       rb.freezeRotation = true; //freezes rotation when in the air
}