- Home /
How to use rigidbody.AddForce in a single frame.
Hi guys, I'm making a gun drop away from the player but when I apply the rigidbody.Addforce on it, it doesnt stop moving, I have everything working fine, the possible solution is to make the "AddForce" works like when we call it on "void Start () {}", I just dont understand why it still updating the command even if I close it in a simple "If"
I'm using "rigidbody.AddForce (transform.forward * 20);"
I already tried to use a coroutine to fix my problem:
public IEnumerator ThrowWeapon()
{
Weapon.rigidbody.AddForce (transform.forward * 20f);
yield return new WaitForSeconds(0.25f);
Weapon.rigidbody.AddForce (transform.forward * -20f);
}
this works almost fine, the only problem is that the object never sleep, and I dont want to waste memory
I feel like I'm forgetting something, maybe I'm just tired. if I find a solution I post here, Peace off.
SOLUTION:
I had to make a condition to the object so when its touching the ground I call rigidbody.velocity = Vector3.zero, it was effective, and the transform of the object in the inspector stopped as I wanted, I didnt use the rigidbody.AddForce in a single frame but at least I could solve my main problem.
wait a second, you said that the object are never sleep, does it meaning that the object are always moving and never stop?
Yes, the transform of the object in the inspector doesnt sleep, as it was receiving force yet, I just wanted to break that, this doesnt happen if I put my function in the "void start(){}", cuz the force is applied in the first frame, and you know its a gun dropping action, I should be able to do that whenever I want.
Thanks hexagonius I used what you said to make the rigidbody stop and it worked, finally my object stopped shaking in the ground.
Answer by hexagonius · Mar 05, 2015 at 05:36 PM
Is the object even coming to a rest? (lying still)
Did you apply a PhysicsMaterial to the gun that has friction? (makes it stop by itself)
If its just one gun forget about the performance
If you want the rigidbody to "STOP" call rigidbody.velocity = Vector3.zero on it.
Answer by DrBibop · Feb 20, 2016 at 10:05 AM
@Nikes You could also use an Impulse force instead of a constant one (if I understand what you are trying to do). You can add "ForceMode.Impulse" to your line :
Weapon.rigidbody.AddForce (transform.forward * 20f, ForceMode.Impulse);
If you do so, I think the two other lines would be useless.
Your answer
![](https://koobas.hobune.stream/wayback/20220613185107im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
My script for pick up items work is wrong! Help please 1 Answer
In need of help with OnTriggerEnter 0 Answers
How do I control the speed of a rotating rigidbody? 0 Answers