How to make a collision script

I am pretty new to scripting, and i wanted to make a shooting demonstration scene. I have the weapons, the reloading system, etc… But do anyone know what do i need to write in a script that would make the following:

I have an enemy, and it obviously have colliders. I have a bullet that is nothing more than an invisible cube with physics and his own collider. I want to make something that when my bullet hits the enemy, it plays an animation. How do i do that? I have a script that i made, but as i don’t know what to write next, i don’t know if it would fit.

#pragma strict

var Bullet:Rigidbody;
var itsahit = false;

function OnTriggerExit (o:Collider) {
    itsahit = true;
}

    function OnTriggerEnter (o:Collider) {
        if (itsahit == true) {
            GetComponent.<Animation>().Play();
        }
    }

OnTriggerEnter happens before OnTriggerExit, so your “itsahit” flag might not work as you expected. I’m also not sure why you need it anyway. Usually you’d need only OnTriggerEnter and perform some action immediately.

Having said that, for fast moving projectiles a collider might not be the right tool anyway, because it’s possible that it just passes through the collider without any hit.

Therefore a Physics.Raycast is often used instead: