- Home /
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
Answer by thereadypunk · Apr 13, 2021 at 02:35 AM
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
}
Your answer
Follow this Question
Related Questions
Getting cosinus of an angle in degrees 3 Answers
Instantiating an object in front of the player 3 Answers
How to calculate the rotation of a hexagon based on its six vertices position 2 Answers
Forward angles 2 Answers
Rotating with a preferred driection. 0 Answers