- Home /
Huge performance hit when spawning prefab with AI
Hey guys, I'm working on a simple top-down shooting game, and I just started having some trouble when I began working on spawning zombies into the world. Basically I have an array of spawn points, and when I press a button a random point is picked and a zombie is spawned. The zombie prefab has a basic AI script attached to it (it just chases the player at this point). The problem I have is that as soon as the first zombie spawns, my game takes a huge performance hit (140 fps down to 11-13 when the zombie spawns). I can't for the life of me figure out what's happening with it, so any help is appreciated. The zombie AI script is as follows:
#pragma strict
private var target : Transform;
private var moveSpeed : float = 3.0;
private var turnSpeed : float = 3.0;
private var myTransform : Transform;
private var health : float = 100;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
}
function Update()
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), turnSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
I also have a GameController script attached to an empty GameObject in the world, and it controls the spawning of zombies:
#pragma strict
public var zomPrefab : GameObject;
private var roundNumber : int = 1;
private var Zombies : ArrayList;
private var spawnPoints : GameObject[];
function Start ()
{
spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
Zombies = new ArrayList();
// var newZom : GameObject = GameObject.Instantiate(zomPrefab, spawnPoints[0].transform.position, Quaternion.identity);
// Zombies.Add(newZom.gameObject);
// RenderSettings.ambientLight = new Color(.2, .2, .2, 1);
}
function OnGUI()
{
GUI.Label(Rect(0, 0, 50, 100), "Round " + roundNumber);
GUI.Label(Rect(0, 50, 100, 100), "Zombies: " + Zombies.Count);
// GUI.Box(Rect(0, 0, 50, 100), roundNumber);
}
function Update ()
{
if(Input.GetKeyDown("tab"))
{
NewRound();
}
}
function NewRound()
{
roundNumber++;
SpawnZombie();
}
function SpawnZombie()
{
var spawnPoint : int = Random.Range(0, spawnPoints.length);
Debug.Log("Spawning zombie at point " + spawnPoint);
var newZom : GameObject = GameObject.Instantiate(zomPrefab, spawnPoints[spawnPoint].transform.position, Quaternion.identity);
Zombies.Add(newZom.gameObject);
}
I know that Instantiate() calls are expensive, but I figured that since I'm not using many that shouldn't be a huge problem. Any thoughts?
Thanks!!
Two points:
First, your zombie AI is using Quaternion.Slerp when it should probably be using RotateTowards.
Second, a quick once-over turns up nothing elaborate in these scripts. I suspect the problem is something else.
Try something like this:
Play your game in the editor
Spawn a zombie
Pause the game
Disable one or two components on the zombie
Unpause the game
Has frame rate improved? If yes, you found the problem component. If no, repeat steps 3-6 for another component.
Other than what rutter said, what platform are you targeting and is the FPS drop in the editor or in a build? (mobile, PC, Win8 app, etc).
Do the zombies have a rigidbody on them and have physics going?
Do the draw call numbers go up by chance when you spawn a zombie? (even if you don't have pro you can see this data in the editor, draw calls, poly count, etc)
Try doing a debug statement that tells you the number of zombies in the arraylist in case for some reason it's spawning more than once zombie at a time (and they overlap eachother, maybe?)
I did what rutter suggested and tried toggling components on the spawned zombies. They only have 2 components (the Zombie script and a capsule collider), and the performance issue seems to stem from having them both active at the same time. Disabling either or both of the components fixes the framerate issue. I'm not sure why this is, though. Any help?
Thanks for the earlier comments by the way, both of you!
EDIT: The zombies do not have a rigidbody on them, and the only physics present is the capsule collider. The FPS drop is in both editor and builds (PC) and draw call numbers don't increase markedly when a zombie is spawned.
Scripts are complete? No OnCollisionStay call?
What happens if collider is made a trigger?
Does it start in a collision? Does the other object have OnCollisionStay routine? $$anonymous$$esh collider - Convex? $$anonymous$$arked as static etc etc?
DropBox it if you want. I or someone will take a look. Don't think its a scripting problem.
After I get out of class I will try to clean up the project so it's small enough to zip and upload.
The scripts don't have anything weird like OnCollisionStay calls or anything, and if the collider is made a trigger the performance is still bad. The framerate doesn't drop when the object collides, it drops as soon as the first zombie is spawned.
I tried using a RigidBody ins$$anonymous$$d of a collider on the prefab, which resulted in much better performance. However, I'm not quite sure how to make that look the best. $$anonymous$$y model currently is not animated, and I know that with a rigidbody I can check for RaycastHit (my shooting system involves Raycast with a LineRenderer).
I don't have time to mess with it currently (about to go to class), but my question is: will using a RigidBody on the enemy zombies introduce any issues as far as animating the zombie while it runs/attacks? Will it need a collider to keep from falling through the terrain?
Thanks for the help!
Answer by Jeff-Kesselman · May 27, 2014 at 08:36 PM
If you have Pro then your first step in diagnosing any performance problem should be to run the Profiler.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150029im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Engine issue... 1 Answer
Zombie wave spawn help? 0 Answers
¿can i draw Objects only when in a certain range around the Player? 2 Answers
Problem with spawning script 1 Answer
Unity Code Performance Issue 1 Answer