Is there any "Performance" errors on this script?

Hi there!! ^^
I made this simple EnemyAI for a game concept that i am currently developing … For the moment the enemies aren’t anything else than simple cubes with a Rigidbody attached to them … They respond to Physical properties and collides with other objects on the Scene … The thing is that whenever the Attack function starts the enemy starts to moving a little … let’s say “Laggy” o.O The weirdest thing is that this occurs only at certain diagonal angles while chasing the player.
It is difficult to explain it :confused: … I think it has something to do with the Quaternions … but as i barely made it to get those horrible things into my script i have no idea if they are “Compatible” or the best option to get along with the rest of the code … Any ideas? =)

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
	public Transform player;
	float distancefrom_player;
	public float look_range = 20.0f;
	public float agro_range= 10.0f;
	public float move_speed= 5.0f;

	void Update () 
	{
		distancefrom_player = Vector3.Distance (player.position, transform.position);
	}

	void LateUpdate(){
		
		if (distancefrom_player < look_range ) 
		{
			transform.LookAt(player);
		}
		
		if (distancefrom_player < agro_range) 
		{
			attack();
		}
		
	}
	
	void lookAt()
	{
		Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
		transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime);
	}
	
	void attack()
	{
		transform.Translate (Vector3.forward * move_speed * Time.deltaTime);
	}


}

Right off the bat I would change this:

  if (distancefrom_player < look_range ) 
         {
             transform.LookAt(player);
             if (distancefrom_player < agro_range) 
             {
                  attack();
             }
         }

That way you are only checking one statement if it is out of bounds

What you also could do is use a collider to see if the user is inside of the look range instead of having to do a distance call each frame that subtracts 2 vectors, it’s the diff between one bit check and math. Other than that there is not much more you can do. Do a profiler test on 100 of these and see what you get with a collider and distance.

Replace the distance comparison to distance squared comparison so it doesnt need to call expensive square roots every time:

distanceFromPlayerSqr = Vector3.SqrMagnitude(player.pos - enemy.pos); //(or something along these lines)

Don’t forget to square the aggo range when checking comparing it to distanceFromPlayerSqr. (aggroRange * aggroRange)

Also I’d move that distance check out of update and into it’s own function that is called at an adjustable rate (a “think rate” in this case that you can decide on) using InvokeRepeating():

Start()
{
InvokeRepeating("GetDistance", 0.5f); //Calls function "GetDistance()" every 0.5 seconds.
}

This code may not be exact.