Ideas/Alternatives to Improve this Update Script

Hi

I currently have a whole bunch of characters on screen (30), whose animations I want to change according to the position of the player. At the moment I’m using a script that sits on each Spawned Character (I use pooling for the characters), that changes the animation (I use SpriteManager2 for anims) as so:

function LateUpdate() 
{
	if(playerTransform.position.y > 100) 
	{
		this.characterSprite.DoAnim(1);
		return;
	}
	else 
	{
		this.characterSprite.DoAnim(2);
		return;
	}
}

I tried some code on the player control script instead, and changed the above into a ChangeAnim() function, but I couldn’t get that to work (only one of the characters would change anime, instead of all of them).

I did think about using Broadcast Messages, but I believe that would be more GPU intensive than what I have above.

Essentially, I’m looking for other ideas or advice on how to do the above more efficiently.

Thanks for any advice.

I am a BIG believer in removing scripts once they have done their job. So if you only need to check for the >100 once, Destroy the script after they have finished, but then I suppose that will not be possible in your case. The other option is event listeners, combined with delegate.

Is this showing a particular bottle neck?

So, and idea…

You already have pooling, so you can access all ‘active’ pooled character. So add a script on the player, and whenever the player moves, tell all the active pooled characters the player has moved. Then they only figure out if they need to change anim then.

I’m not a big fan of the idea, but if you notice some major bottlenecking already, this might help.

OR/AND… if the bottle necking is occurring because you say to ‘play’ an anim. Don’t call DoAnim unless the current anim isn’t the one you need to call.

if(dist > 100 notPlayingAnim1) characterSprite.DoAnim(1);
if(dist < 100 notPlayingAnim2) characterSprite.DoAnim(2);

The calls will need to be ongoing, as the player moves around and the enemies respond to that movement, so removing the script isn’t an option unfortunately.

I thought even t listeners were worse in terms of memory?

This second option may be the way t go, yes. I’ll check with Spritemanager 2 though because I thought that saying "DoAnim"rather than “PlayAnim”, means that it only plays the anim if it isn’t already playing…

Which idea are you not a fan of and why exactly?

Thanks to both of you for your help!