Mirza Zeeshan
Mirza Zeeshan

Reputation: 99

Stop animation OnTriggerExit

I am using collider to play my animation onTriggerEnter and want to stop the animation onTriggerExit and play again onTriggerEnter and so on.

Here's the onTriggerEnter script:

var chestSound : AudioClip;
var treasureChest : GameObject; 
function OnTriggerEnter (col : Collider) {
    if(col.gameObject.tag == "Player") { 
        AudioSource.PlayClipAtPoint(chestSound, transform.position);
        treasureChest.animation.Play();
        Destroy(gameObject);
    }
}

Upvotes: 1

Views: 587

Answers (1)

Ori Frish
Ori Frish

Reputation: 411

Well, I would write

function OnTriggerExit (col : Collider) {
 if(col.gameObject.tag == "Player") 
     treasureChest.animation.Stop();
 }

but it seems that maybe your use of Destroy(gameObject) could affecting the game. you are destroying your own player object as he triggers the other object, how could he go through OnTriggerExit?

Upvotes: 2

Related Questions