- Home /
Slow down with multiple enmies using LookAt
Hey guys and girls.
I'm coding a shoot 'em up; and with this I'm finding problems. My main problem is how to move an enemy, when there are 200+ enemies on the screen. It creates serious performance issues. I've optimized my scripts via the docs help; however once there are 100 or so enemies on screen the FPS drops, and for the most part my enemies (cubes at present) look like crap.
The first script, that is really causing a problem is my first enemy script, and thankfully this is the test script.
#pragma strict
static var target : Transform; //the enemy's target
var target2 : Transform; //the enemy's target
static var moveSpeed = 5; //move speed
var rotationSpeed = 5; //speed of turning
var myTransform : Transform; //current transform data of this enemy
var distanceToWall = float;
var life : float =3;
var boom : Transform;
static var baseScore : float = 500;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function FixedUpdate(){
target = GameObject.FindWithTag("Player").transform; //target the player
transform.LookAt(Vector3(target.position.x, target.position.y, 0));
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
Here's a screen grab of my Profiler:
I've never worked with so many enemies, and moving objects before.
The enemies are rigidbody, though they only need to recognize a collision between the player or a bullet.
I'm quite lost, as I've been using this kinda code to move objects for a while - and well, it's never been a problem, but then never on this scale.
Any help is greatly appreciated.
I don't think you are going to want to call GameObject.FindWithTag("Player").transform for 200 hundred enemies. Try removing that just to see if there is any performance improvement in the profiler. Your probably going to want to assign that in the inspector.
Also, perhaps you may want to try to look at the player every second or two ins$$anonymous$$d of a fixed update.
Answer by Sirex · Sep 15, 2012 at 10:13 AM
I have not worked with that problem but i can try to provide some optimisation methods.
First you could cache GameObject.FindWithTag("Player") in Start.
Second you could fetch player transform with 1 second intervall, maybe randomise so not all agents fetch at the same frame.
Instead of making lookat every frame you could use the stored transform and make an angle calculation and decide on a treshold value, only when this threshold has been exceeded do you make a lookat proceduer.
You could use a ghost class that list enemies according to their positions and calculate lookat and set it to several agents which could use roughly the same values.
Data driven design, you have a ghost class that calculate the lookat for all the agents in its own update function. The rationale being that if you call the same code multiple times from the same place you will gain processing power seance the cpu don't have to calculate lookat one time then execute a lot of code to switch to anouther object and do the same thing.
Answer by DesiQ · Sep 15, 2012 at 10:15 AM
FindWithTag, GetComponent, and other variants that use strings to find a game object are extremely expensive. I found it funny that you were caching the enemy's transform 'for performance' (I see no reason to do so, since the transform will just change once the enemy moves around), but not caching the player's gameobject reference, which will never change. :p
I suspect he is just caching the transform component for reference. Which unity recommends in the documentation, and not the actual transforms position.