x


Why is this script causing a lot of lag?

Hello, I have been trying to create a simple enemy AI for a first person shooter. For some reason my AI script causes a lot of lag. Here is the script:

var players:GameObject[];
var player:GameObject;
var speed:float;
var hitrange:float = 1.5;
var enemyprefab:Transform;
var healthkitprefab:Transform;
var ammokitprefab:Transform;

var plr:plrstats;
var enemy:enemystats;
var best:int = 0;
var current:int;

var x:int = 0;

var startpos:Vector3 = Vector3.zero;

var canhit:boolean = true;

var isspawned:boolean;

function Start()
{
    isspawned = false;
    startpos = transform.position;
    enemy = gameObject.GetComponent(enemystats);
}

function Update () {
    if (!isspawned)
    {
       spawn();
       return;
    }
    x++;
    players = GameObject.FindGameObjectsWithTag("Player");
    for(var plr:GameObject in players)
    {
       current = Vector3.Distance(transform.position, plr.transform.position);
       if (current > best)
       {
         player = plr;
         best = current;
       }
    }

    if (player == null) return;
    if (Vector3.Distance(transform.position, player.transform.position) > hitrange && !canhit)
    {
       canhit = true;
    }

    if (Vector3.Distance(transform.position, player.transform.position) < hitrange)
    {
       if (!canhit) return;
       plr = player.GetComponent(typeof(plrstats));
       plr.Damage(20);
       canhit = false;
    }

    if (enemy.isdead)
    {
       plr = player.GetComponent(typeof(plrstats));
       plr.points += 10;
       var bonus:Transform;
       bonus = Instantiate(enemyprefab, startpos, Quaternion.identity);
       bonus.name = "enemy";
       var x = Random.Range(1,20);
       if(x == 1){bonus = Instantiate(healthkitprefab, transform.position, Quaternion.identity); bonus.name = "healthkit";}
       if(x == 2){bonus = Instantiate(ammokitprefab, transform.position, Quaternion.identity); bonus.name = "ammokit";}
       GameObject.Find("pointer").guiText.text = "+";
       GameObject.Destroy(gameObject);
    }
}

function FixedUpdate()
{
    if (player == null) return;
    if (canhit)
    {
       transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(player.transform.position-transform.position), speed);
       rigidbody.AddForce((transform.position-player.transform.position).normalized*speed*Time.deltaTime);
    }
}

function spawn()
{
    yield WaitForSeconds(3);
    isspawned = true;
}

There is over 10 enemies on the map when the game is running, each enemy has one of these attached. The lag doesn't appear until the enemies are spawned. When I took the things from the fixed update and put them in update the lag was replaced by the enemies not moving as they should. Could the lag be caused by fixedupdate?

How could I prevent the lag?

Thank You.

more ▼

asked Aug 28 '12 at 02:33 PM

Dachckol gravatar image

Dachckol
9 2 5

for starters, this is odd:

if (!isspawned)
{
   spawn();
   return;
}


function spawn()
{
    yield WaitForSeconds(3);
    isspawned = true;
}

the yield means you are calling the spawn() function every frame for three seconds, which means a lot of simultaneous WaitForSeconds are running (not sure if this would cause a lot of lag)... and since "isspawned" doesn't seem to be used in this sample, I wonder where else it is being used...

either way, that could probably be in the Start:

function Start()
{
    isspawned = false;
    startpos = transform.position;
    enemy = gameObject.GetComponent(enemystats);
    yield WaitForSeconds(3);
    isspawned = true;
}
Aug 28 '12 at 03:09 PM Seth Bergman

Thanks for the reply. I used the awake function and the lag is practically gone. The cause of the rest of the lag is probably the amount of enemies there is on the map.

If anyone sees anything else that is inefficient and will slow the application down please tell me.

Aug 28 '12 at 03:19 PM Dachckol
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Some things: you shouldn't do any Find commands, such as FindGameObjectsWithTag, in Update. Do it once and cache the results. Also your logic causes a new instance of the spawn function to be launched every frame for 3 seconds. Avoid checking things every frame when they only happen once, such as "isdead"...instead of having this in Update, make it a function that's called once when necessary. Finally, does this really need to be in Update at all? Update is for things that happen every frame; it seems likely that it really only needs to be done a few times per second maybe, so use InvokeRepeating or coroutines instead of Update.

more ▼

answered Aug 28 '12 at 03:21 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

Thank you! the lag is now completely gone.

Aug 28 '12 at 03:30 PM Dachckol
(comments are locked)
10|3000 characters needed characters left

your culprit is x++ why you need it any way, even if you need it you should have controlled its value somewhere.

Also, cut this "players = GameObject.FindGameObjectsWithTag("Player");" from update() and paste it in Start(), its not a good idea to find players continuously in update()'s as its not going to give you two different result than calling it once at start()

more ▼

answered Aug 28 '12 at 03:19 PM

Sundar gravatar image

Sundar
581 2 2

Thanks for the reply.

Aug 28 '12 at 03:30 PM Dachckol
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1174
x960
x653
x196
x12

asked: Aug 28 '12 at 02:33 PM

Seen: 434 times

Last Updated: Aug 28 '12 at 03:30 PM