Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Suppapa · Sep 04, 2015 at 12:59 PM · scripting problemphysicsrigidbodyvelocityscriptingbasics

When I try to save the velocity of a rigidbody and use it later it doesn't work?

I'm trying to have both a freeze time and turn gravity off mechanic where I can shoot something off without gravity, freeze it, and then unfreeze it and have it still moving but when I try to freeze the rigid body and save the velocity it doesn't apply it later. my code:

 using UnityEngine;
 using System.Collections;

 public class ObjectPhysics : MonoBehaviour {
 Vector3 savedVelocity;
 Vector3 savedAngularVelocity;
 private Rigidbody rb;
 public ObjectChanger script;
 GameObject target;
 void Start () {
     rb = GetComponent<Rigidbody>();
     target = GameObject.FindGameObjectWithTag("Player");
     script = target.GetComponent<ObjectChanger>();

 }
 // Update is called once per frame
 void Update () {
         if(script.time == false){
             savedVelocity = rb.velocity;
             savedAngularVelocity = rb.angularVelocity;
             rb.isKinematic = true;
     }

         else{
             rb.isKinematic = false;
             rb.WakeUp();
             rb.AddForce(savedVelocity);
             rb.AddTorque(savedAngularVelocity);
     }

     if(script.gravityAndFriction == false){
             rb.useGravity = false;
             rb.angularDrag = 0;
             rb.drag = 0;
     }
         if(script.gravityAndFriction == true){
             rb.useGravity = true;
             rb.angularDrag = 0.05f;
             rb.drag = 0;
     }
 }
 }

  
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

· Add your reply
  • Sort: 
avatar image
0

Answer by _dns_ · Sep 04, 2015 at 02:03 PM

Hi, when using AddForce with default second parameter ( = ForceMode.Force) , Unity will compute a velocity using force, mass and (fixed) deltaTime. This will not result in the velocity you saved. Setting the second parameter to ForceMode.VelocityChange should restore the velocity (well, I think it will add it to existing velocity). You could also simply set the velocity to the saved value, the same way you read and saved it. The same thing apply to angular velocity.

The other thing is to do all that during FixedUpdate. The physics engine will compute a step after FixedUpdate so it's better to always modify physics related values during FixedUpdate. The reason is that there may be 2 or more calls to Update between 2 calls to FixedUpdate (and then to the physics engine), depending on framerate and physics time settings.

Comment
Add comment · Show 2 · 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 Suppapa · Sep 04, 2015 at 11:26 PM 0
Share

For the first part where it say's Force$$anonymous$$ode.VelocityChange where should I put that

avatar image _dns_ · Sep 04, 2015 at 11:30 PM 0
Share

There is a second optional parameter to AddForce (see documentation) but the best will be to just set the velocity directly with the value you saved, I do this all the time and the physics engine accepts it without problems (have to be more careful with positions, but velocities are ok)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Add velocity relative to ground? 1 Answer

How do I reduce the bounciness of a ball affected by gravity in .js? 2 Answers

Stoping Velocity Breaks Script [Solved] 0 Answers

Throwing objects with a fixed joint? 1 Answer

Move Ball With Same Velocity on Slope 1 Answer


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