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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Object keeps moving after hit 0 Answers

Lerp to target position in X amount of time 2 Answers

Why object goes sometimes through walls by adding force ? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges