Ok I have an AI script that works (so far), except for the obstacle avoidance. What I want is, when one of the raycast hits an object for it to turn and then continue to head towards the target (which may turn from the next waypoint to the player). The problem is that the enemy jitters in a certain direction and then tries to go to the waypoint. Also is there anyway I can write this in less code? I’m pretty noob at complex stuff like this, the collision detection was horribly written but sort of works aside from the jittering instead of turning and then trying to go to the waypoint. Here is the code, it’s very long:

```
var rotationSpeed = 5;
var moveSpeed = 5;
var maxHealth = 100.0;
var curHealth = 100.0;
var healthBarLength : float;
var waypoint : GameObject[];
var currentWaypoint : int;
var maxWaypoint : int;
var target : GameObject;
var startingDrag : float;
var groundDistance : float;
var isGrouned : boolean;
var isObstructed : boolean;
function Start(){
healthBarLength = Screen.width / 4;
distToGround = collider.bounds.extents.y;
waypoint = GameObject.FindGameObjectsWithTag ("Waypoint");
System.Array.Reverse(waypoint);
startingDrag = rigidbody.drag;
isObstructed = false;
}
function Update(){
target = waypoint[currentWaypoint];
var fwd = transform.TransformDirection (Vector3.forward);
var l25D = Quaternion.Euler(0,-25,0) * transform.forward;
var r25D = Quaternion.Euler(0,25,0) * transform.forward;
var l50D = Quaternion.Euler(0,-50,0) * transform.forward;
var r50D = Quaternion.Euler(0,50,0) * transform.forward;
var left1 = Quaternion.LookRotation(l25D);
var right1 = Quaternion.LookRotation(r25D);
var left2 = Quaternion.LookRotation(l50D);
var right2 = Quaternion.LookRotation(r50D);
var targetPos = target.transform.position;
var targetRotation = Quaternion.LookRotation(targetPos - transform.position);
var i = Time.deltaTime * rotationSpeed;
var m = Time.deltaTime * moveSpeed;
var relativePos = targetPos - transform.position;
if(isObstructed == false){
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, i);
rigidbody.AddForce(m * relativePos.normalized);
}
if(currentWaypoint >= maxWaypoint){
currentWaypoint = 0;
}
if (Physics.Raycast (transform.position, fwd, 3)) {
}
if (Physics.Raycast (transform.position, l25D, 3)) {
isObstructed = true;
transform.rotation = Quaternion.Slerp(transform.rotation, right2, i);
rigidbody.AddForce(Vector3.right2 * m);
}else
Invoke ("turn", 4);
if (Physics.Raycast (transform.position, l50D, 3)) {
isObstructed = true;
transform.rotation = Quaternion.Slerp(transform.rotation, right1, i);
rigidbody.AddForce(Vector3.right1 * m);
}else
Invoke ("turn", 4);
if (Physics.Raycast (transform.position, r25D, 3)) {
isObstructed = true;
transform.rotation = Quaternion.Slerp(transform.rotation, left2, i);
rigidbody.AddForce(Vector3.left2 * m);
}else
Invoke ("turn", 4);
if (Physics.Raycast (transform.position, r50D, 3)) {
isObstructed = true;
transform.rotation = Quaternion.Slerp(transform.rotation, left1, i);
rigidbody.AddForce(Vector3.left1 * m);
}else
Invoke ("turn", 4);
}
function Health() {
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 4) * (curHealth / maxHealth);
}
function OnGUI() {
GUI.Box(Rect(1050, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
function OnDrawGizmos () {
var fwd = transform.TransformDirection (Vector3.forward);
var l25D = Quaternion.Euler(0,-25,0) * transform.forward;
var r25D = Quaternion.Euler(0,25,0) * transform.forward;
var l50D = Quaternion.Euler(0,-50,0) * transform.forward;
var r50D = Quaternion.Euler(0,50,0) * transform.forward;
// Draws a 5 meter long red line in front of the object
Gizmos.color = Color.red;
var direction : Vector3 = transform.TransformDirection (Vector3.forward) * 3;
var direction2 : Vector3 = l25D * 3;
var direction3 : Vector3 = l50D * 3;
var direction4 : Vector3 = r25D * 3;
var direction5 : Vector3 = r50D * 3;
Gizmos.DrawRay (transform.position, direction);
Gizmos.DrawRay (transform.position, direction2);
Gizmos.DrawRay (transform.position, direction3);
Gizmos.DrawRay (transform.position, direction4);
Gizmos.DrawRay (transform.position, direction5);
}
function OnTriggerEnter(info : Collider){
if(info.tag == "Waypoint"){
currentWaypoint++;
yield slow();
}
}
function slow(){
rigidbody.drag = 100;
yield WaitForSeconds(.1);
rigidbody.drag = startingDrag;
}
function turn(){
isObstructed = false;
}
```