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 Coaster-Mind · Apr 21, 2016 at 09:16 AM · c#addforcemax

AddForce LimitSpeed with acceleration and deceleration?

I've a script on a object that is controlled with sliders. Ive got a maxspeed control slider that controls the speed but when I change the speed down the object responds directly and goes to that speed that is given. But I want a nice deceleration instead of a direct reaction.

Is it possible to make a Maxspeed, that if it changes during gameplay via a slider, it changes the value of the AddForce to 0 until its given speed is reached and then setting the Maxspeed to the NewMaxspeed.

Or are there other solutions? Please explain them because I learn best that way. Also use examples so I can train my skill.

Thanks for reading this and it would be great to get help.

-CoasterMind

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

Answer by Naphier · Apr 21, 2016 at 09:47 AM

First, get the rigidbody's velocity (http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html). Check to see if the sqrMagnitude of that velocity is greater than your max speed (http://docs.unity3d.com/ScriptReference/Vector3-sqrMagnitude.html), If the velocity is too high then you have some options to slow it down.

Option 1 - increase the drag until the velocity is below your set mark. This will act like the brakes on a car. Add more and more drag to stop faster.

Option 2 - Get the inverse of the vector using gameObject.transform.InverseTransformDirection(velocity) (http://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html). Then use that inverted velocity vector to add force to your object until it is slowed.

Option 3 - Calculate the velocity using Vector3.Lerp (http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html). This one's a bit more complicated since you'll need to take the current velocity, normalize it, and then multiply it by the desired magnitude velocity. That's the endpoint of the lerp. This will, however give you control over the amount of time the deceleration takes place.

Another option would be to try to stop at a certain position. This one's a bit complicated and would require lerping the position over the proper deceleration time. I'm not sure if you can do this and make it look very realistic, so I'd say use one of the other methods and if you really need to stop at a specific point it might be better to not use Unity's physics engine :P

As for providing you with examples, they're mostly in the manual. You just need to write the code based on the above principles and learn. Unity's manual has a lot of great info and you might find the Vector Cookbook section of some use (http://docs.unity3d.com/Manual/VectorCookbook.html).

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 Coaster-Mind · Apr 21, 2016 at 10:14 AM 0
Share

Will test later today,

It sounds promising although I don't understand scripts fully yet.

Edit: This if for a ship. A cruise ship so the motion needs to be very slow.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

How to create parabola path for addforce() 2 Answers

How to make a ball stay in the air longer when force is added? 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