I’m working on a shooting program where every time the object shoots an animation is played. Because the shooting animation is activated from the Update() function, I can’t just put in shotsFired ++ as it would scale up a huge amount as it would be increased every frame. I tried shotsFired += 1 * Time.deltaTime however that didn’t work either. How do you suggest I keep count of the number of times an enemy has fired?
Has lots of tutorials for making shooting game. But for this little problem of yours, you should increase numbers when any triggering occurs, like shooting the gun.
I also recommend that you buy and read Will Goldstone’s book:
I’ve read the book twice, and I tried simply increasing the counter however because the trigger occurs as the result of a distance check that happens in Update() the number goes crazy high in only a couple seconds.
As I said it is played from update().
I have:
“shotsFired ++;
Animation.play…”
Which is in a function that is called by another function that checks distance. The checkDistance() function is called every frame from Update(). Thus, shotsFired goes up every frame.
[/quote]
You can set a boolean flag the first time the animation is triggered and then use this to avoid incrementing shotsFired until the animation has finished:-
var shooting: boolean;
function Update() {
...
if (!shooting checkDistance()) {
shooting = true;
animation.Play("Shoot");
shotsFired++;
} else {
shooting = animation.IsPlaying("Shoot");
}
...
}