Comments and answers for "Rotate a 2D Rigidbody to a desired angle using AddTorque?"
http://answers.unity.com/questions/1131048/rotate-a-2d-rigidbody-to-a-desired-angle-using-add.html
The latest comments and answers for the question "Rotate a 2D Rigidbody to a desired angle using AddTorque?"Answer by manesh
http://answers.unity.com/answers/1163375/view.html
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);
}Thu, 31 Mar 2016 03:34:20 GMTmaneshComment by CoolCasualCat on CoolCasualCat's answer
http://answers.unity.com/comments/1131595/view.html
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$$Fri, 22 Jan 2016 20:59:14 GMTCoolCasualCatAnswer by MelvMay
http://answers.unity.com/answers/1131314/view.html
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][1], 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][2] using the formula:
[Rigidbody2D.angularVelocity][3] += [Time.fixedTimeDelta][4] x (1 / [Rigidbody2D.inertia][5]) 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][6] is used so it should be set to zero if you don't require such drag.
The angular velocity changes the [Rigidbody2D.rotation][7] using the formula:
[Rigidbody2D.rotation][8] += [Time.fixedTimeDelta][9] x [Rigidbody2D.angularVelocity][10]
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][11] 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][12]. 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][13]) from the starting angle to the target angle.
[1]: http://docs.unity3d.com/ScriptReference/Rigidbody2D.AddTorque.html
[2]: http://docs.unity3d.com/ScriptReference/Rigidbody2D-angularVelocity.html
[3]: http://docs.unity3d.com/ScriptReference/Rigidbody2D-angularVelocity.html
[4]: http://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html
[5]: http://docs.unity3d.com/ScriptReference/Rigidbody2D-inertia.html
[6]: http://docs.unity3d.com/ScriptReference/Rigidbody2D-angularDrag.html
[7]: http://docs.unity3d.com/ScriptReference/Rigidbody2D-rotation.html
[8]: http://docs.unity3d.com/ScriptReference/Rigidbody2D-rotation.html
[9]: http://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html
[10]: http://docs.unity3d.com/ScriptReference/Rigidbody2D-angularVelocity.html
[11]: http://docs.unity3d.com/ScriptReference/Rigidbody2D-angularVelocity.html
[12]: http://docs.unity3d.com/ScriptReference/Rigidbody2D.MoveRotation.html
[13]: http://docs.unity3d.com/ScriptReference/Mathf.Lerp.htmlFri, 22 Jan 2016 13:26:38 GMTMelvMay