x


Collision Objects, JavaScript

I would like to know how i can access the object i am colliding with to figure out how my object should react.(trying to make AI) for instance if it hits a player i want it to start attacking but if it hits a wall it will just turn in another direction.

more ▼

asked Jan 22 '11 at 07:09 AM

Unity user gravatar image

Unity user
17 4 5 10

You could do an if loop or a switch based on collision.collider.tag

Jan 22 '11 at 01:15 PM azzogat
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Put this in your AI script:

function OnTriggerEnter (other : Collider) {
    if (other.gameObject.CompareTag ("Player")) {
        //start attacking
    }
}

If you're not using a trigger, it'll be something like:

function OnCollisionEnter (collision : Collision) {
    if(collision.Collider.gameObject.CompareTag("Player")){
        //start attacking
    }
}
more ▼

answered Aug 27 '12 at 08:17 AM

gribbly gravatar image

gribbly
390 1 2 5

(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:

x3461
x2499
x960
x153

asked: Jan 22 '11 at 07:09 AM

Seen: 1299 times

Last Updated: Aug 27 '12 at 08:17 AM