- Home /
[SOLVED] Leave footprints/footsteps behind an enemy
Hi.
I have been wondering for a while, how do I make an enemy leave footprints behind him, where he walked. I have been thinking of doing an invisible enemy, with no model at all, and he would only leave footprints (textured planes) behind him.
This is what I got so far:
var footprintLeft : Transform;
var footprintRight : Transform;
var footstepSound : AudioClip;
var transformRotation : Quaternion;
function Update () {
transformRotation = Quaternion(transform.rotation.x, -transform.rotation.y, transform.rotation.z, 0.0);
//in the actual script, I check here if the enemy is chasing me, simply by using a boolean that activates when chasing
DoFootprints();
}
function DoFootprints () {
//here I put the if statement that checks if the enemy is currently chasing the player (in the actual AI script)
yield WaitForSeconds(1);
Instantiate(footprintLeft, transform.position, transformRotation);
audio.PlayOneShot(footstepSound);
yield WaitForSeconds(1);
Instantiate(footprintRight, transform.position, transformRotation);
audio.PlayOneShot(footstepSound);
yield WaitForSeconds(1);
DoAgain();
}
function DoAgain () {
DoFootprints();
}
I did the Quaternion thing, because the footstep textures were turned the wrong way. As of now, the script makes footprints and sounds, but it makes them too quickly. Maybe because I am calling it in the Update() function?
Answer by Eric5h5 · Jun 17, 2012 at 07:24 PM
You're creating a new instance of DoFootprints every frame in Update, and on top of that, you made it recursive so that each instance calls itself forever (making another function called DoAgain that calls the DoFootprints function won't bypass that, it's just an unnecessary step). You should remove Update entirely, and use an infinite loop instead of endless recursive functions, since those will eventually run out of stack and crash.
Also it would be better to make a footprint system that combined them into one mesh, since having a separate object for each footprint will get pretty slow. Or at least have a limited pool of objects and recycle them so that older ones are replaced.
I'm sorry, could you please give me a link to the reference or an example? I don't know how to loop in scripts any other way.
You could do something like this:
function OnEnable () {
InvokeRepeating("DoFootprints", 1.0, 1.0);
}
function OnDisable () {
CancelInvoke("DoFootprints");
}
function DoFootprints () {
if (animation["walking"].enabled) {
//$$anonymous$$ake some foot prints here
}
}
I think it still calls it every frame. When I do it your way, it still keeps doing it too fast. But thanks for the help, I definitely learned something new.
What the example does is when you activate the script or object a call each second is being made to execute DoFootprints(). If it still happens every frame you still have something that calls DoFootprints() in Update() or a yielded loop.