Search Unity

Stop a rigidBody on a collision

Discussion in 'Scripting' started by George S, Aug 24, 2009.

  1. George S

    George S

    Joined:
    Jul 28, 2009
    Posts:
    54
    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):
    1. using UnityEngine;
    2.  
    3. public class BulletForce : MonoBehaviour
    4. {
    5.   public Vector3 shootForce;
    6.   public bool shootMe = false;
    7.  
    8.   void Update () {
    9.     if (shootMe) {
    10.       rigidbody.AddForce (shootForce, ForceMode.Acceleration);
    11.       shootMe = false;
    12.     }
    13.   }
    14.  
    15.   public void  OnCollisionEnter(Collision collision) {
    16.     Debug.Log("BulletForce collided with " + collision.gameObject.name);
    17.     rigidbody.velocity = Vector3.zero;
    18.     rigidbody.angularVelocity = Vector3.zero;
    19.     rigidbody.Sleep();
    20.   }
    21.  
    22.   public void setForce(Vector3 theForce) {
    23.     shootForce = theForce;
    24.     shootMe = true;
    25.   }
    26. }
    27.  
    I added angularVelocity and Sleep based on suggestions from querying this forum.

    Any ideas on how to get the bullet to stop.
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  3. George S

    George S

    Joined:
    Jul 28, 2009
    Posts:
    54
    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?
     
  4. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    Try this:

    Code (csharp):
    1.  
    2. rigidbody.AddForce(-rigidbody.velocity, ForceMode.VelocityChange);
    3.  
     
    WoodyDRN likes this.
  5. HanulTech

    HanulTech

    Joined:
    Apr 5, 2009
    Posts:
    312
    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.