[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?

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.