This site uses strictly necessary cookies. More Information

X- Home /

# Rotate a 2D Rigidbody to a desired angle using AddTorque?

In my game I have several rigidbodies that interact with eachother using physics. The objects will be rotating, but I can't get the objects to rotate to any desired angle because the angles just don't seem to be reliable for any calculations (to my level of knowledge).

Is there anyway I could make a 2d PID controller so then the rigidbody will rotate properly to a given angle.

Or could I at least get some help on making calculations with angles.

**Answer** by MelvMay
·
Jan 22, 2016 at 01:26 PM

I think the best way I can help you is to describe how, when applying a single torque value, it is used during the next fixed-update.

When you call Rigidbody2D.AddTorque, the value you use is added to an internal torque value and is only used (and subsequently reset back to zero) during the fixed-update.

During the fixed-update, this accumulated torque value (or only a single one if you only called AddTorque once) will be added to the Rigidbody2D.angularVelocity using the formula:

Rigidbody2D.angularVelocity += Time.fixedTimeDelta x (1 / Rigidbody2D.inertia) x Torque

It's worth pointing out that the angular velocity of a body will stay at this value unless a force acts on the body to change it and/or Rigidbody2D.angularDrag is used so it should be set to zero if you don't require such drag.

The angular velocity changes the Rigidbody2D.rotation using the formula:

Rigidbody2D.rotation += Time.fixedTimeDelta x Rigidbody2D.angularVelocity

In simpler terms; the torque value is what modifies the angular velocity of the body by being multiplied by the time-step and the inverse body rotational inertia. This angular velocity changes the body rotation by being multiplied simply by the time-step.

Alternately, if you don't want to use forces and directly manipulate the Rigidbody2D.angularVelocity then everything becomes much simpler. Simply setting the angular velocity means setting a value in degrees/second the object will rotate. If you calculate how many degrees (positive or negative) you need to move then simply set the angular velocity and wait for the appropriate amount of time. This methods makes it easy to set an angular speed (it's the angular velocity itself).

Another way is to use Rigidbody.MoveRotation. After you've calculated how many degrees you need to move (as above) then simply call the above method during fixed-update by linearly interpolating (Mathf.Lerp) from the starting angle to the target angle.

wait so the angular velocity will be dependant on the torque? shouldn't it be the other way around? like the torque should be proportional to the difference between the target and current angular velocity, and the target angular velocity will be proportional to the difference in the target and current angle of the rigidbody?

Sorry if I totally missed something obvious. I'm trying to understand all this at 2A$$anonymous$$

**Answer** by manesh
·
Mar 31, 2016 at 03:34 AM

Here's my quick and dirty solution, after being flummoxed trying to figure this out. Using Unity 5.4b12.

The most important thing I figured out is `rotation`

is in degrees. It can go above 360 (e.g. 720, 1080, etc.) as well as be negative (-360, -720, etc.), depending on how the object was rotated. Some simple math makes it consistent.

```
float angle = this.rigidbody2d.rotation % 360;
float torque = 0.25f;
if(angle < 0)
angle += 360;
if(angle < 1f || angle > 359f)
{
this.rigidbody2d.transform.rotation = Quaternion.identity;
}
else if(angle > 180f)
{
this.rigidbody2d.AddTorque(torque);
}
else
{
this.rigidbody2d.AddTorque(-torque);
}
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.