- Home /
Playing an animation after killing a mob?
Hey guys once again i need the help of this wonderful community. My question is as follows,
i have it set up so that a bunch of mobs come to the player and the player needs to kill them,so i want it so when you kill the last guy it triggers an animation. please help so that i may proceed with my game thanks in advance!
Answer by Kourosh · Nov 28, 2010 at 05:46 PM
The algorithm i can think of is to:
1.Create PlayAnimation.js. apply this to the object with the animation.
static var nMobs:int; //holds the amount of killed mobs.
var totalMobs:int = 10; //Total number of mobs.
function Update() {
if (nMobs ==totalMobs ){
animation.Play("nameOfTheAnimation"); //Play desired animation.
nMobs = 0; //prevent from playing animation every frame.
}
}
2.Create CountMobs.js. apply this to your mobs prefab. I assume that a single collision could kill them.
function OnCollisionEnter(collision : Collision) {
//you can use the collision information stored in the collision variable to verify the correct hit. you may learn that here: [Collision][1]
PlayAnimation.nMobs++; //Adds 1 to the number of killed mobs in the first script.
}
function OnCollisionEnter(collision : Collision) { //you can use the collision information stored in the collision variable to verify the correct hit. you may learn that here: [Collision][1] PlayAnimation.n$$anonymous$$obs++; //Adds 1 to the number of killed mobs in the first script. }
When i made that script its telling me that unknown identifier : 'PlayAnimation'.
Ok, make sure both scripts are in a same location inside your project folder. I just tested if I put one inside the standard assets and the other one somewhere outside that folder i get the same error as yours.
by the way. the link to collision references: http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html?from=Collision
Okay now i got it to work the problem was that the first script needed to be called PlayAnimation lol.Dont know why but when i changed the name to PlayAnimation i pwned the error.$$anonymous$$ourosh if it wasn't for your help i'd still be stuck, thanks to you and this great community we fixed the problem hopefully one day i'll know as much as you =) thanks again and happy game making !
very glad it helped :) all i know is form unity scripting manual and this community.