|
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: 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.
(comments are locked)
|
|
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. Thank you! the lag is now completely gone.
Aug 28 '12 at 03:30 PM
Dachckol
(comments are locked)
|
|
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() Thanks for the reply.
Aug 28 '12 at 03:30 PM
Dachckol
(comments are locked)
|

for starters, this is odd:
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:
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.