- Home /
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
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).
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
Follow this Question
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