- Home /
Move Towards Angle with AddTourque (C#)
Now, here's something which will require some complex math.
I have a Rigidbody object which I want to rotate to a certain degree by adding torque to accelerate its angular velocity, and then when it gets close to its targeted rotation, apply torque in the opposite direction until it fully slows down and stops right at the targeted rotation. (Targeted rotation is going to be changed constantly with mouse, keyboard, or joystick controls.)
Here's what I found as an answer to a similar question, but it doesn't apply torque as it comes closer to the targeted rotation:
if (transform.eulerAngles.y != targetRotation.eulerAngles.y) {
if (targetRotation.eulerAngles.y - transform.rotation.eulerAngles.y > 180) {
m_Rigidbody.AddTorque (0, (targetRotation.eulerAngles.y - transform.rotation.eulerAngles.y - 360), 0, ForceMode.Acceleration);
} else if (targetRotation.eulerAngles.y - transform.rotation.eulerAngles.y < -180) {
m_Rigidbody.AddTorque (0, (targetRotation.eulerAngles.y - transform.rotation.eulerAngles.y + 360), 0, ForceMode.Acceleration);
} else {
m_Rigidbody.AddTorque (0, (targetRotation.eulerAngles.y - transform.rotation.eulerAngles.y), 0, ForceMode.Acceleration);
}
}
A good example would be Reassembly ship control and handling (Although instead of gyroscopes, it uses thrusters to rotate).
Edit:
Looks like my question is about a PID controller. Here's what I found:
http://forum.unity3d.com/threads/spaceship-control-using-pid-controllers.191755/
http://forum.unity3d.com/threads/pid-controller.68390/
Any help would be appreciated! Thank you.
Answer by DemSec · Aug 12, 2016 at 10:38 PM
I'm looking more into this:
http://answers.unity3d.com/questions/197225/pid-controller-simulation.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612084431im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Flip over an object (smooth transition) 3 Answers