1
\$\begingroup\$

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.

enter image description here

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)

enter image description here

Can collider do anything with this problem?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.

enter image description here enter image description here


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;
}
\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$
    – user75648
    Commented Jan 23, 2016 at 8:06
  • \$\begingroup\$ I want dynamic rotation(more like tilt)because I'm using asymmetrical/ uneven platform see my edited question. \$\endgroup\$
    – user75648
    Commented Jan 23, 2016 at 9:03
  • \$\begingroup\$ Tried your code but the final angle that i get is 180degrees and my player gets locked to that rotation. \$\endgroup\$
    – user75648
    Commented Jan 24, 2016 at 6:54
  • \$\begingroup\$ @user75648 Then it sounds like you might need a different value for standardRotation. \$\endgroup\$ Commented Jan 24, 2016 at 7:33
  • \$\begingroup\$ Im using -90 In standardRotation. Because my player flips up side down when 90degrees is used. But either way the rotation freezes though. \$\endgroup\$
    – user75648
    Commented Jan 24, 2016 at 8:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .