Script Oprimization Problem

I want a large amount of enemies at once in my game, each enemy has a script.
When I try to instantiate 200 enemies at once I have a serious FPS drop.
I’ve found that if I turn off the enemy script the game just flys.
After optimizing the script I still have FPS drops in this part of my script:
//Calc rotation

			newRotation = Quaternion.LookRotation(target.position - transform.position);

			

			//Dont look up or down

			newRotation.z = 0;

			newRotation.x = 0;

			
			if(distanceToTarget < 100)
			{
				//Rotate slowly
				transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 4);
			} 
			else
			{

				//Rotate instantly if the target is too far

				transform.rotation = newRotation; 
			}

			

			//Move towards target 

			transform.Translate(0, 0, speed / 100, Space.Self);

If I coment this part of the script the game works fine even with 500 enemies!
Can someone explain whats wrong with it?!

You can’t simply zero newRotation.x and z: newRotation is a quaternion, and its xyzw components have nothing to do with the familiar angles we know. If you want to use only the horizontal direction, kill the y component in the vector:

var lookDir = target.position - transform.position;
lookDir.y = 0; // keep the direction horizontal
newRotation = Quaternion.LookRotation(lookDir);

I don’t know if this will improve the FPS, but for sure the game will work better this way.

Ok, I’m making this a proper answer, because I’ve really got a lot of points to make.

This is basically why you shouldn’t be using transform.Translate as well as rigidbody physics- you should use either one, or the other, and they both have quite different techniques for controlling an object’s position. If you are relying on the Physics system to simulate gravity, but are otherwise managing all the object’s movement yourself, maybe you should just manage gravity manually as well and do away with the rigidbodies entirely? By the looks of things, your ‘speed’ value ignores physics as it is, so it’s kind of absurd to have one small aspect of your object obeying the laws of physics while the rest of it exists outside of any ‘physical’ system.

Also, if you require collisions, there are even more problems here- Transform.Translate doesn’t check for collisions, it just moves the object regardless of if it would intersect with something (because, as I’ve said before, it works independently from physics). This will lead to even more weird behaviour, because in every FixedUpdate the object would try to extricate itself from any objects which it has been embedded in by the Transform.translation, causing constant jittering.

If it is that important that you use Rigidbody components, you should rework your movement script so that it uses forces and torque instead of translation and rotation. It’s somewhat more complex to code for, but allows you to use the full power of they physics engine without anything interfering with anything else.

Otherwise, just do gravity by hand (i.e, by translating downwards by a certain amount every frame) and get rid of the rigidbody.

Another thing, I don’t know if Aldo has mentioned it or not, but using

transform.whatever

is a little slow, since ‘transform’ is actually a lookup function which automatically finds the local transform instance every time you call it. If you need to squeeze the performance out of it, you could cache it using

var myTrans : Transform = transform;

and then whenever you would use ‘transform’, use ‘myTrans’ instead to save on the lookup time. You can apply the same logic to ‘rigidbody’ and even ‘gameObject’ to speed things up.