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.