- Home /
The question is answered, right answer was accepted
Pausing and Resuming a Rigidbody
I have this code:
void FixedUpdate()
{
bool r = false;
if(PauseManager.inst.isPaused)
{
if(!r)
{
PrePauseVel = rigidbody.velocity;
Debug.Log("Stored Vel: " + PrePauseVel);
PrePauseAngVel = rigidbody.angularVelocity;
Debug.Log("Stored AngularVel: " + PrePauseAngVel);
rigidbody.isKinematic = true;
r = true;
}
}
if(!PauseManager.inst.isPaused)
{
if(rigidbody.isKinematic == true)
{
rigidbody.isKinematic = false;
rigidbody.velocity = PrePauseVel;
Debug.Log("Velocity: " +rigidbody.velocity);
rigidbody.angularVelocity = PrePauseAngVel;
Debug.Log("Angular Velocity: " +rigidbody.angularVelocity);
r = false;
}
}
}
i expected this to allow me to pause the rigid body in it's position when the game pauses and resume when the game unpauses but for some reason. according to the debug log the saved velocity is correct but when the velocity is reapplied it is still 0,0,0. am i missing something.
Answer by MattBaranowski · Mar 18, 2013 at 02:58 PM
I ran into the same problem recently and found this question without an answer so far. The solution that worked for me was to use the Rigidbody AddForce and AddTorque function with ForceMode.VelocityChange options. I think the reason is that when you set isKinematic to true the first time the Rigidbody is put to sleep. Simply setting the velocity does not wake it up, but adding a force does. Here is the code that worked for me.
Vector3 savedVelocity;
Vector3 savedAngularVelocity;
void OnPauseGame() {
savedVelocity = rigidbody.velocity;
savedAngularVelocity = rigidbody.angularVelocity;
rigidbody.isKinematic = true;
}
void OnResumeGame() {
rigidbody.isKinematic = false;
rigidbody.AddForce( savedVelocity, ForceMode.VelocityChange );
rigidbody.AddTorque( savedAngularVelocity, ForceMode.VelocityChange );
}
Edit: After finding my original solution above I noticed that there is also Rigidbody.WakeUp that would also solve the above problem. I'm not sure which method is better.
this is what I eventually figured out, forgot to close this, but thanks for the help, also I just set the velocities directly and it worked fine i did change something to make it work, don't remember what thought..
rigidbody.AddForce( savedVelocity, Force$$anonymous$$ode.VelocityChange ); rigidbody.AddTorque( savedAngularVelocity, Force$$anonymous$$ode.VelocityChange ); . CS1501: No overload for method AddForce' takes
2' arguments why this isnt working ?
Hi folks! Just passing by to let you know that I've published a study in my blog that uses this technique and this post is credited there.
Check out: http://fredzvtgamedev.com/2014/07/full-study-split-screen-with-pause/.
Thanks!
Thank you for your help, but I got an error saying that the ForceMode.VelocityChange is not compatible with unity 2d and I needed to use ForceMode2D but can't seem to find the VelocityChange in ForceMode2D
Thanks in advance :)
Follow this Question
Related Questions
Rigidbody.velocity giving weird result 0 Answers
Velocity for movement 0 Answers
C# Rotate velocity issue 1 Answer
Need some help with firing projectile at player using Rigidbody Velocity 1 Answer