Too many creatures slows performance on screen

I just finished coding some zombie AI, but when I start to put more than 5 zombie prefabs on the map, my performance slows to about 20 fps from 60 fps. Does anyone have a clue why this is?

The stats showed about 100k polys on screen which is alot, but my laptop should be able to handle it. I suspect a lack of LOD on my level, but am not sure how to turn that on (if it’s there)

It’s probably poorly optimized code, not polycount. This can be easily determined by bringing the 5 zombie prefabs into the scene with their AI scripts disabled and see if you get the same fps drop.

And of course it’d be impossible to help optimize your code without seeing it.

Thanks, I figured that it was this function doing it. It’s code I got from someone else here. It worked fine until you add too many pawns.
I think it’s the sqrMagnitude, which is slow to calculate(i think that’s the term)

function sight()
{
	var direction : Vector3 = target.position - transform.position;
	var sigit = Vector3.Angle(direction, transform.forward);
	var eatDist = Vector3.Distance(target.position, transform.position);
	
	//wanted to get the zombie to see within an angle of player
	if(direction.sqrMagnitude < sightDistanceSQ   sigit < sightAngle)
	{
	
		if(eatDist<=deathdist)
		{
			AttackF();
		}
		else
		{
			MoveTowards(target.position);
		}
		print("I see you");
	}
	else 
	{
		animation.CrossFade(Idle.name);
	
	}	
	print("direction "+direction);
}

comment out the lines that print, and see if that improves your performance.

You may use a AI Manager script, that calls all updates to NPCs referenced in a list.

(also, if you use a lot of sqr, you can use a invsqrt (fast sqrt) function… But may only work on 32bits though)

Resolving: It was the print statements. What a major pain!!!