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!!