Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 13, 2013 at 11:24 PM by TheDarkVoid for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by TheDarkVoid · Jul 13, 2012 at 11:33 PM · c#rigidbodyvelocitypausing

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheDarkVoid · Mar 18, 2013 at 11:04 PM 0
Share

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..

avatar image SNSD · Jun 24, 2014 at 11:26 AM 0
Share

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 ?

avatar image FredZvt81 · Jul 21, 2014 at 03:01 PM 0
Share

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!

avatar image PhoenixWorldDev · Dec 28, 2021 at 01:01 PM 0
Share

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Player Sprint button not working properly... 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges