- Home /
Countering AddForce by Modifying velocity
I'm currently working on an object grabber mechanic using AddForce to move the grabbed object. However, when the camera too fast, the grabbed object will have a enormous speed and fly in a direction. To counter this, I want to limit the grabbed objects velocity. However, it seems changing its velocity has no impact at all on the object.
Here's a simplified Example :
grabbedObject.rigidbody.AddForce(force);
grabbedObject.rigidbody.velocity = Vector3.zero;
What I expect is the second line to set the velocity to zero, but it doesn't. I thought I might be applying a force somewhere else in the code, but when I remove the first line, there is no mouvement at all, which mean that the mouvement is actually all coming from the first line. I thought AddForce just modified the rigidbody's velocity, but why is it still moving then if I put that velocity to 0 ?
EDIT : Putting the code on the object that is being grabbed instead of the object that is grabbing seems to work. I'm not sure how yet and would like an alternative.
I don't know why your code is not working, but to limit your velocity, you can add the following in FixedUpdate():
grabbedObject.rigidbody.velocity = Vector3.Clamp$$anonymous$$agnitude(grabbedObject.rigidbody.velocity, maxVelocity);
...where 'maxVeolcity' is replaced by a constant or is a variable that you initialize.
Yes, I had something similar to that, though Clamp is faster. I didn't put it here since even putting the velocity to zero had no effect on the object's mouvement.
Answer by Sprawl · Jun 29, 2014 at 03:47 PM
Just wanted to post an update on this in case someone has a similar issue.
I still haven't found a way to correctly modify the velocity of the grabbed object with my Controller script that allow grabbing. I think it might have something to do with execution order but I found an alternative even though it's not as clean and I don't want to waste more time on this.
What I've done is actually putting a new script on grabbed object that clamp their velocity. It requires a new script on each grabbable objects, but I don't have so many and it is all pretty simple so it works in my situation.
Answer by Kiwasi · Jun 18, 2014 at 07:46 PM
Are you running this code in FixedUpdate?
Thing to remember with forces is they are queued and don't actually get applied until fixed update. So if this code is in the Update loop you could be adding the force after you set the velocity to zero.
Answer by AngryShamrock42 · Dec 03, 2014 at 06:07 PM
This is because AddForce does not actually modify velocity immediately. Instead, it queues a velocity modification to happen later, during the rest of the physics step. I'm not sure what the exact time line is, but I'd guess it happens just after fixedStep() finishes.
So the affect of AddForce is actually occurring after your velocity modification. Changing the velocity to 0 only removes the velocity the rigidbody had before the current step.
I'm not sure what the best way to go about resolving this is. Probably by replacing AddForce with a direct velocity modification, or by only conditionally adding the force.
Late, but I figured you might still be curious as to what caused this.
That would make sense. I dont quite remember all the details of the fix I made, but thanks for answering anyway .
Your answer
Follow this Question
Related Questions
Why does writing to rigidbody.velocity after AddForce stop my rigidbody moving? 4 Answers
How can I convert velocity/direction to Force? 3 Answers
AddForce vs Velocity issues with Rigidbody2D 2 Answers
Throwing knife doesn't throw correctly 0 Answers
Recieving a nullpointerexception while following the Unity Instantiation tutorial 1 Answer