# How to get dynamic RigidBody rotation/tilt on uneven platform?

I have a rigid body with PolygonCollider2D which has some complex circular shape and I'm using uneven platform.

Im using Following code to move the object forward

CharacterRigidBody.velocity = new Vector2 (speed, CharacterRigidBody.velocity.y);


Problem : The rigid body keeps on rotating when it moves forward.

I want the character to move without rotation on asymmetrical platform(above).But i don't want to freeze rotation because i want the character to tilt when climbing or decending platform.

My RigidBody2D config(I'm using unity 5.1.2)

Can collider do anything with this problem?

It you look at the Rigidbody or (Rigidbody2D) in the inspector under Constraints > Freeze Rotation you will see a checkbox for each applicable axis. These checkboxes will stop the physics engine from applying any rotation about that axis.

Now that we have stopped the physics engine from managing the rotation we can control it ourselves by manipulating either Rigidbody.rotation or Rigidbody2D.rotation.

A Raycast can be used to do a quick search for any colliders below the player. RaycastHit.normal can then be examined to determine the slope of the collider.

Here is a sample implementation:

void FixedUpdate()
{
// my test sprites were sometimes misaligned
const float standardRotation = 90f;

// we want to ignore any colliders more distant than this
const float rayDistance = 1f;

// default angle
float angle = standardRotation;

// do we have any ground under us?
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, rayDistance);
if (hit)
{
// what is the angle of the ground?
angle = Mathf.Atan2(hit.normal.y, hit.normal.x) * Mathf.Rad2Deg + standardRotation;
}

// set our rotation to this angle
_rigidbody2d.rotation = angle;
}

• see my edited question i don't have an option to freeze different axis I'm using RIgidBody2D as i mentioned i don't want to freeze rotation. Jan 23, 2016 at 8:06
• I want dynamic rotation(more like tilt)because I'm using asymmetrical/ uneven platform see my edited question. Jan 23, 2016 at 9:03
• Tried your code but the final angle that i get is 180degrees and my player gets locked to that rotation. Jan 24, 2016 at 6:54
• @user75648 Then it sounds like you might need a different value for standardRotation. Jan 24, 2016 at 7:33
• Im using -90 In standardRotation. Because my player flips up side down when 90degrees is used. But either way the rotation freezes though. Jan 24, 2016 at 8:28