- Home /
How to detect collision with a rigidbody?
I'm trying to figure out how to detect the collision of a rigidbody (a bullet) with another object, such as a player, a target (not necessarily a rigidbody, unless it needs to be). How would I do this?
Update: I tried using this code:
function OnCollisionEnter(collision : Collision)
{
Destroy (gameObject);
}
It was meant to destroy the bullet (where the script is contained), but it didn't work. I'm not sure if this matters but, the bullet is a rigid body with collision detection set to continous. What it's colliding with isn't a rigidbody, and it has a mesh collider on it. But the thing is that what I'm trying to make the bullet detect the collision on is imported, and has a mesh collider on it, not a box collider. So what would I do about that?
Bullets tend to move too fast for collision detection to be infallible changing your rigid body's collision type to continuous dynamic if it moves or continuous if it doesnt can help but the increase of collision check frequency increases load on your processor thats why its usually a better idea to use a raycast to detect whether or not the bullet hit. furthermore its also possible implement penetrating shots if you use raycastall.
Answer by Aleron · Oct 14, 2011 at 03:39 AM
There's a basic example in the documentation: http://unity3d.com/support/documentation/ScriptReference/Rigidbody.OnCollisionEnter.html
Basically, you implement your behavior in the OnCollisionEnter() method for your bullet rigid body.
Answer by Dhruvil · Oct 14, 2011 at 06:56 AM
Use OnTrigger instead of onCollision... It would be much better to used in arcade game collision...
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html
Your answer
Follow this Question
Related Questions
Why my capsule collider sometimes stucks in other colliders? 0 Answers
Having a kinematic rigidbody detect collision with a collider without a rigidbody 7 Answers
Static, Non movable rigidbody 4 Answers
Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers
Problem with CharacterController collision detection 1 Answer