- Home /
Question by
Unity user · Jan 22, 2011 at 07:09 AM ·
javascriptcollisionaiaccess
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.
Comment
You could do an if loop or a switch based on collision.collider.tag
Answer by gribbly · Aug 27, 2012 at 08:17 AM
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
}
}