- Home /
Slowly bring rigidbody back to position?
How can I get a rigidbody to slowly return to it's original position while it's still in motion? Here's the motion script I have right now
// rotation speed var rotSpeed : float = 8;
function FixedUpdate ()
{
var rotationSpin : float = Input.GetAxis("Horizontal");
var rotationFlip : float = Input.GetAxis("Vertical");
rigidbody.AddRelativeTorque ((rotationFlip)* rotSpeed, (rotationSpin) * rotSpeed, 0);
}
Sorry if the code didn't format properly
What do you mean 'return'? Do you want the object to always get pulled back to its starting position after it gets moved? Do you want the force to be constant?
I want my character to always slowly rotate back to it's original position.
I only want the the "flip" to be affected though, so if he rotated a bit, he'll flip back to his original position but still be rotated. I think I can accomplish that part by having the target follow the rotation or something like that. I made a little diagram of what I mean by that
I tried using slerp but it creates a jittery effect.
Answer by chainedlupine · May 01, 2012 at 04:15 AM
You could do something like this:
void Update ()
{
// Whatever axis you want to align to
Vector3 worldAxis = new Vector3 (0, 1, 0) ;
// rotate towards this axis
Vector3 worldAxisRelative = transform.TransformDirection (worldAxis) ;
axisAlignRot = Quaternion.FromToRotation (worldAxisRelative, worldAxis) ;
transform.rotation = Quaternion.Slerp (transform.rotation, axisAlignRot * transform.rotation, 1.0f * Time.deltaTime) ;
}
This works exactly the way I want but is there a way to do it with rigidbodies? Or should I just completely move away from physics? Every time I do some sort of other transformation I get a jittery effect. Thanks for the reply!