- Home /
to stop a rigidbody for a moment (works like a pause, only it's only for this object)
if I want the rigidbody to stop for a moment (think of it as freezing in time, only this object, while others are still working) I should save it's velocity and angular velocity on FixedUpdate and restore it on FixedUpdate as well, correct?
the the thing is - I need the object to always react the same - imagine us going back in time after the object is frozen (but before it's unfrozen) and each time we should see the "unfrozen" rigidbody reacting the same. Acting just as it was (and as it would be, if it wasn't frozen);
so in part-pseudocode - I assume - restoring velocity and angularVelocity in fixedUpdate like
function Update(){ if press a button //freeze yield WaitForFixedUpdate; saveVelocity = rigidbody.velocity; saveAngularVelocity = rigidbody.angularVelocity; rigidbody.isKinematic = true;
if press a button for the second time //unfreeze yield waitForFixedUpdate rigidbody.isKinematic = false; rigidbody.WakeUp(); rigidbody.velocity = saveVelocity; rigidbody.angularVelocity = saveAngularVelocity
} ?
would do the trick, correct?
Answer by duck · Nov 07, 2010 at 09:50 PM
Looks fine to me. Probably no need for the yields.
A fleshed-out version might look something like this:
var paused = false; var saveVelocity : Vector3; var saveAngularVelocity : Vector3;
function Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
// toggle pause state
paused = !paused;
if (paused) {
saveVelocity = rigidbody.velocity;
saveAngularVelocity = rigidbody.angularVelocity;
rigidbody.isKinematic = true;
} else {
// un-paused
rigidbody.isKinematic = false;
rigidbody.velocity = saveVelocity;
rigidbody.angularVelocity = saveAngularVelocity
rigidbody.WakeUp();
}
}
}
Using Get$$anonymous$$eyDown (or any Get*Down function from Input) in FixedUpdate doesn't work, since the Down event only occurs during the single frame that it's true, which--depending on the framerate and physics framerate--probably won't be the same frame that FixedUpdate runs. The end result is that pressing buttons only sometimes works, so Get*Down must be checked every frame, such as in Update.
so yielding for FixedUpdate in a "freezing" and "unfreezing" functions might be a good idea?
Oh - Eric5h5 is right, sorry, those kind of inputs and FixedUpdate don't mix. I'm going to edit the answer to use Update ins$$anonymous$$d! But either way, I don't think you need to use a "yield" statement at all, unless I'm misunderstanding your problem. (why would you need one?)
I thought the actual saving (and restoring) properties should take place in FixedUpdate, as it concerns rigidbodies -and physics - and I was told it's the proper place to operate on physics. also, because I actually did it in Update but rigidbodies of a ragdoll that I saved that way - didn't restore well - that means they sometimes behaved different on each restore (and they acted different than they do when they aren't frozen - and restored). and I really need them to behave like nothing happened - and behave each time the same way. so I thought I'll ask for ideas.
also, I thought that yield WaitForFixedUpdate would get the input well (as it's still on the Update) but will wait till FixedUpdate, to perform operations on physics/rigidbodies.