x


Have bullets be destroyed when they collide with ANY collider

Hi,

Was wondering if there is a way to have a bullet (with collider) be destroyed when it hits any object's collider (enemy/wall, etc), rather than specifying an individual collider.

What's the best method to use? OnCollisionEnter, OnTriggerEnter, Ray casts, and how?

Thanks

more ▼

asked May 20 '12 at 05:27 PM

asimov gravatar image

asimov
157 19 36 40

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

hopefully I'm not mistaken in saying that this will check for any collisions with the bullet

function OnCollisionEnter(){
     Destroy (gameObject);
}

Hope that works! Scribe

more ▼

answered May 20 '12 at 05:41 PM

Scribe gravatar image

Scribe
1.4k 12 16 34

NOTES:
1- place this code in the bullet's script;
2- the bullet must be a rigidbody.

May 20 '12 at 05:44 PM aldonaletto

@aldonaletto

Correct me if I am wrong, but the bullet does not need a rigidbody as long as the gameObject it touches is a rigidbody, right?

May 20 '12 at 05:50 PM OrangeLightning

@OrangeLightning : I almost asked that also lol ... but figure since they want the bullet to destroy itself when it hit's "anything" , then it's either the bullet has a rigidbody, or would have to make sure "everything" else has a rigidbody on it.

May 20 '12 at 05:54 PM Lo0NuhtiK

Yes, in theory at least one of them must be a rigidbody - but moving a non-rigidbody object do not awake the hit rigidbodies (the object passes through them), thus it's better to make the bullet a rigidbody.

May 20 '12 at 06:13 PM aldonaletto

Actually it's important that only one of the elements has a rigidbody. I f the bullet has one and the wall (for example) has one too, the collision will not be detected. If both have a rigidbody, one has to be convex

May 20 '12 at 06:23 PM KiraSensei
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2507
x764
x337
x309
x227

asked: May 20 '12 at 05:27 PM

Seen: 1243 times

Last Updated: May 21 '12 at 12:32 AM