1
\$\begingroup\$

I'm trying to make a rigidbody2D circle return to an orientation after a collision. But there is a weird behavior I do not expect - it always orients to the same direction.

This is what I call in FixedUpdate():

rotationdifference = -halfPI + rigidbody2D.rotation;
rigidbody2D.AddTorque (rotationdifference * ease);

I would expect this would rotate 90 degrees (1/2 Pi Radians) off of the neutral axis. But it does not. In fact it performs exactly the same as:

rotationdifference = rigidbody2D.rotation;
rigidbody2D.AddTorque (rotationdifference * ease);

What is going on? How would I be able to set an angle I want it to ease towards, and then have it ease towards it when its not colliding with some other force?

=====

Edit

For those who want to try this themselves, here is a MonoBehavior. Apply it to a sprite with a rigidbody2D. I have a boxcollider going througha field of pegs like in pachinko.

using UnityEngine;
using System.Collections;

public class MarchingMovement : MonoBehaviour {

public Vector2 speed = new Vector2(50, 50);
public float ease = 0.01f;
private float rotationdifference;
private float halfPI = 1.57079632679f;
// Use this for initialization
void Start () {
    //rotationdifference = 3.0f;
}

// Update is called once per frame
void Update () {

}
void FixedUpdate()
{
    // 5 - Move the game object
    rigidbody2D.velocity = speed;
    rotationdifference = -halfPI + rigidbody2D.rotation;
    rigidbody2D.AddTorque (rotationdifference * ease);
}
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.