I have a bullet that shoots it self using AddForce on it's rigidBody. When the bullet hits something I want it to stop, not bounce. I thought setting the velocity to zero would work but it does not. Here is my code. Code (csharp): using UnityEngine; public class BulletForce : MonoBehaviour { public Vector3 shootForce; public bool shootMe = false; void Update () { if (shootMe) { rigidbody.AddForce (shootForce, ForceMode.Acceleration); shootMe = false; } } public void OnCollisionEnter(Collision collision) { Debug.Log("BulletForce collided with " + collision.gameObject.name); rigidbody.velocity = Vector3.zero; rigidbody.angularVelocity = Vector3.zero; rigidbody.Sleep(); } public void setForce(Vector3 theForce) { shootForce = theForce; shootMe = true; } } I added angularVelocity and Sleep based on suggestions from querying this forum. Any ideas on how to get the bullet to stop.
You can always set it to rigidbody.isKinematic = true; You'll have to set it back to false if you wanted it to move again, though.
But why does it not work? Using "rigidbody.isKinematic" = true the bullets still bounce and the stop in mid-air a fair distance from where they hit. Is there a good way to stop an rigidbody object that has been set in motion using a force?
You can stop it instantly by setting the rigidbody's velocity to zero. Make sure of course, you don't do this and then continue applying any force in any subsequent frame or it won't work. The other thing to keep in mind is that above a certain velocity, the trigger/or collision detection event that causes the event handler to be called in which you are setting the velocity to zero may not fire. There are several posts on this topic. The exact velocity required before this happens depends on multiple factors such as the speed the projectile is moving at, the width of the collider being impacted and performance of the hardware on which the code is executing. For velocities that look good, though this is usually not an issue.