AI causing framerate issues

Ok so here’s my enemy ai code, with about 30 enemies on screen on my mac, the framerate went down to about 130, at it’s worst it went to about 90, is there something wrong with my code? Am I using raycasting wrong? Or is there a better way to go about this? Any help would be great thanks.

var player : Transform;
var action : int;
var rechaseRange : float = 200.0;
var attackRange : float = 150.0;
var health : float = 20;
var speed : float = 5.0;
var raycastLength : float = 10.0;
var LRRaycast : float = 2.0;
var playerPos : Vector3;
var shoot : boolean = true;
var bullet : Transform;
var gun : Transform;
var bulletSpeed : float = 200.0;

function Update () {
playerPos = player.transform.position;
playerPos.y = transform.position.y;
var right = transform.TransformDirection(Vector3.right);
var left = transform.TransformDirection(Vector3.left);
var fwd = transform.TransformDirection(Vector3.forward);
var range = Vector3.Distance(transform.position, player.position);
var point : Vector3 = player.position;
var myPoint : Vector3 = transform.position;

if(range >= rechaseRange){
//if range is greater than chaseRange, action = 1.
action = 1;
}
else if(range <= rechaseRange){
if(range <= attackRange){
//if range is less than chaseRange, and if range is less than attack range, action = 2.
action = 2;
}
}
if(health <= 0){
//if health is less than or equal to 0, action = 3.
action = 3;
}

switch (action) {
case 1:
print("One Find");
if (!Physics.Raycast(transform.position, fwd, raycastLength)  
!Physics.Raycast(transform.position, right, LRRaycast)  
!Physics.Raycast(transform.position, left, LRRaycast)){
transform.LookAt(playerPos);
transform.Translate(Vector3.forward * speed);
}
else if (!Physics.Raycast(transform.position, fwd, raycastLength)) {
transform.Translate(Vector3.forward * speed);
}
else if (!Physics.Raycast(transform.position, right, LRRaycast)) {
transform.Translate(Vector3.zero);
transform.Rotate(0,30,0);
}
else if (!Physics.Raycast(transform.position, left, LRRaycast)) {
transform.Translate(Vector3.zero);
transform.Rotate(0,-30,0);
}
break;

case 2:
print("Two Attack");
transform.LookAt(playerPos);
transform.Translate(Vector3.zero);
if(shoot){
shoot = false;
Fire();
}
break;

case 3:
print("Three Die");
Destroy(this.gameObject);
break;

default:
print("None");
}
}

function Fire(){
var IBullet = Instantiate(bullet, gun.position, transform.rotation);
yield WaitForSeconds(1);
shoot = true;
}

First, print and Debug.Log are very very slow functions.

Second thing you might try is, every time you access an object’s transform property, Unity does a little overhead calculation. Store a reference to it and use that instead and you may notice a small increase in speed.

A couple more things…

In a few places, you use transform.Translate but with a parameter of Vector3.zero. This will not move the object so you may as well leave these calls out altogether. Also, Vector3.Distance is surprisingly slow since it involves a square root operation. You can avoid this by taking the square of the distance between the two points and comparing it against the square of the threshold distances:-

var sqrDist = (transform.position - player.position).sqrMagnitude;
var sqrRechaseRange = rechaseRange * rechaseRange;

if (sqrDist >= sqrRechaseRange) {
  ...
}