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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Jan 08, 2017 at 03:29 PM · unity 5rigidbodyvelocityaddforceangularvelocity

How to set rigidbody velocity and angularVelocity to Vector3.zero over time?

Hey!

I'm currently working on a vehicle system for my game, and when you accelerate, the vehicle's rigidbody adds a thrust force, but when you hit 'S' to brake, it stops inmediatly (I'm setting velocity and angularVelocity to zero)

Here's the code:

 if (carReverse == false) {
                     if (Input.GetKey (KeyCode.W) && carCurrentSpeed <= carMaxSpeed) {
                         vehRb.AddForce (Vector3.forward * carThrust);
                     } else if (Input.GetKey (KeyCode.W) == false && Input.GetKey (KeyCode.S) == false && carCurrentSpeed >= 0.03f) {
                         vehRb.AddForce (Vector3.forward * -carThrust);
                     } else if (Input.GetKey (KeyCode.W) == false && Input.GetKey (KeyCode.S) && carCurrentSpeed >= 1f) {
                         vehRb.velocity = Vector3.zero * Time.deltaTime;
                         vehRb.angularVelocity = Vector3.zero * Time.deltaTime;
                     }
                 }

I tried putting 'Time.deltaTime' to see if it would stop as time passed but it instantly stops the vehicle, and by the way this isn't the classic W to drive forward and S to drive backwards, this thing I'm trying to do is to hit 'W' to increase speed either forward or backwards depending if 'carReverse' is true or false, and the S key is only to stop.

Thanks in advance.

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
Best Answer

Answer by KoenigX3 · Jan 09, 2017 at 09:48 PM

You could multiply the velocity and angularVelocity with a float between 0 and 1. By doing this, the vectors would slowly become smaller and smaller, until they get to a critical point, where you should set them to zero.

Note that if you want to use this method with Time.deltaTime to make it frame rate independent, the new float should be bigger than 1 to compensate Time.deltaTime, and the product should be around 0.95 - 1, or something like that.

 vehRb.velocity *= Time.deltaTime * slowingScale;
 vehRb.angularVelocity *= Time.deltaTime * slowingScale;
 
 if(vehRb.velocity.magnitude < criticalValue) vehRb.velocity = Vector3.zero;
 if(vehRb.angularVelocity.magnitude < criticalValue) vehRb.angularVelocity = Vector3.zero;

This solution is easy to implement, however it is not realistic - it is for calculating friction and other things. You will slow down too fast at the beginning, and too slow at the end. You may want to use a velocity-independent mode of braking.

For example, you could try to decrease the length of the vector by a fixed value. You can achieve this by getting the current magnitude, normalizing the vector, then applying the decreased magnitude again.

 vehRb.velocity = vehRb.velocity.normalized * (vehRb.velocity.magnitude - slowingScale);
 
 if(vehRb.velocity.magnitude < criticalValue) vehRb.velocity = Vector3.zero;

The only thing that changes here is that the slowingScale has to be figured out by experimenting.

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Jan 09, 2017 at 10:24 PM 1
Share

Thank you! This was the answer I was looking for. I've tried it and it doesn't matter to me that it looks unrealistic(right now) since the game isn't kinda simulator or such. But thanks for your answer, it is what I was looking for.

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

Sphere with AddForce or Velocity not moving 1 Answer

How do I create Sling-Shot like propulsion? 1 Answer

AddForce instead of modyfing Velocity 4 Answers

Bullets falling through the terrain 2 Answers

Having a OnTriggerEnter make another gameobject Add Force in UnityScript. 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