- Home /
What is the best way to apply force, then come to a smooth stop on control release?
Using keyboard/etc control I'd like to be able to apply a smooth acceleration to a maximum speed, using force if that is the best way to do it. When the control is released, I want to slow down to a stop.
Big note: All of this is done using physics, not transform.
The question is a little ambiguous, even though I don't $$anonymous$$d a good "whats the best way / most efficient way" type question every now and again. When you put force in the title, that is pretty much referring to rigidbodies for everybody here (I assume!).
Now the action you are taking about is friction, in my understanding. So now you have 2 considerations : how to set up the character with rigidbody and physics material? Do i 'fake' physics? The second choice is there because ultimately if you choose the first then there is no absolute control of the object. You can push it around but you will find it very hard to say 'go there this fast and stop exactly there' with forces. There is however a neat relationship between transform.position and rigidbody.velocity. Say you movement script using transform is
transform.position += transform.forward * speed * Time.deltaTime;
now note this can be written for the rigidbody velocity as such
rigidbody.velocity = transform.forward * speed;
So if you want to have any certain control on what your rigidbody is up to, treat it like a transform but then directly set the velocity. $$anonymous$$uch better that getting into crazy calculations with getting the velocity anyway and making a bunch of calculations to reduce the amount of addForce to slow the object down at the target etc etc .....
the very best way to apply force is with AddForce.
the very best way to apply drag is to apply drag.
Contents/Documentation/Documentation/Components/class-Rigidbody.html
awesome!
Yes, don't get me wrong. For realizm use AddForce, then modify the amount of force by distance to target, cap force being applied by checking if the velocity magnitude is exceeding a set amount then don't apply force or make the force amount zero. But from what little I could deter$$anonymous$$e from this question, the OP wanted precision, I merely suggested an alternate method. $$anonymous$$aybe search for rigidbody character controllers and see how they do it :
http://wiki.unity3d.com/index.php/PhysicsFPSWalker
http://wiki.unity3d.com/index.php/RigidbodyFPSWalker
http://wiki.unity3d.com/index.php/Scripts/Physics
Unless they are just talking about calculating when to lerp the amount of force being applied, to stop at a target position. It is too hard to tell.
right on -- yes the Q is a mystery to me, I just felt like typing some generalist help :)
some people don't realize DRAG is there for them..
Answer by Griffo · Nov 20, 2012 at 11:03 AM
Completely off-target. As far as I can tell that's using transform to move, not force, which uses different rules. It's also ment for AI, not player.
No offense but can you post anything OTHER then links? It feels more like you are just trying to get votes then to actually answer anything.
Well, excuse me for trying to point you in the right direction, I wont bother next time ..
Your answer
Follow this Question
Related Questions
Character Controller meets Rigidbody 1 Answer
add force to object that has 2 different rigid bodies 0 Answers
How to apply uni-directional force to a rigidbody 0 Answers
Touch controlled ice puck 0 Answers