x


How do I check if an instantiated object collides with a specific game object?

How would I perform a check to see if an instantiated object (like a bullet) were to hit an other game object (like an enemy)?

I am new to this sorry if this question is noobie

Thanks in advance

more ▼

asked Jan 06 '11 at 05:56 PM

AtomicMarine gravatar image

AtomicMarine
504 42 49 70

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

1 answer: sort voted first

You probably want to use tags on the objects you want the collision to interact with, and then check the tag of the object you hit, like this:

function OnCollisionEnter(other : Collision) {
    if(other.gameObject.CompareTag("Enemy"){
        //do stuff
    }
}
more ▼

answered Jan 06 '11 at 06:02 PM

PrimeDerektive gravatar image

PrimeDerektive
3.1k 57 64 84

Thanks mate :) working awesomely

Jan 06 '11 at 07:55 PM AtomicMarine

function OnCollisionEnter(collision : Collision) { Destroy(gameObject); if (collision.gameObject.tag == "Enemy1"){ }

} oops had to modify code alil for on collision enter function above is final code

Jan 06 '11 at 08:04 PM AtomicMarine

hmm... CompareTag does the same thing as that, except it is a little more optimized and is suggested that you use it. http://unity3d.com/support/documentation/ScriptReference/Component.CompareTag.html

Jan 06 '11 at 08:52 PM PrimeDerektive

oh dang, it does, my bad, thanks changed it now

Jan 06 '11 at 09:12 PM AtomicMarine
(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:

x1964
x126
x40

asked: Jan 06 '11 at 05:56 PM

Seen: 1888 times

Last Updated: Jan 07 '11 at 03:26 PM