AI Avoidance Problem

OK this is is kinda working the way I want it to. I place this as an Update Function in my Alien Zombie AI script:

function Update (){

    //The direction vector to our target
    var dir = (target.position - transform.position).normalized;
    var hit : RaycastHit;
    // Check for forward raycast
    if(Physics.Raycast(transform.position, transform.forward, hit, 20)){
        if(hit.transform != transform){
            Debug.DrawLine(transform.position, hit.point, Color.red);
            dir += hit.normal * 50;
    }

}

var leftR = transform.position;
var rightR = transform.position;

leftR.x -= 2;
rightR.x += 2;

if(Physics.Raycast(leftR, transform.forward, hit, 20)){
    if(hit.transform != transform){
        Debug.DrawLine(leftR, hit.point, Color.red);
        dir += hit.normal * 50;
    }
}
if(Physics.Raycast(rightR, transform.forward, hit, 20)){
    if(hit.transform != transform){
        Debug.DrawLine(rightR, hit.point, Color.red);
        dir += hit.normal * 50;
    }
}

var rot = Quaternion.LookRotation(dir);

transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
transform.position += transform.forward * 1 * Time.deltaTime;

}

My Alien Zombies are moving around my terrain a lot better and seem to be able to move around trees a lot better. But they pass through my buildings to get at my FPS player. The video tutorial I followed mentioned that the wall needed to have a box collider on it and I understand the reason for that. But is there a way to get this to work with my "Mesh Colliders" that are already assiged to each part of my buildings?? I would hate to have to go around and add an additional box collider to every part of every structure that they could possibly pass through ;)

EDIT

Morten Do you mean like this:

function Update (){

    //The direction vector to our target
    var dir = (target.position - transform.position).normalized;
    var hit : RaycastHit;
    // Check for forward raycast
    if(Physics.Raycast(transform.position, transform.forward, hit, 20)){
        if(hit.transform != transform){
            Debug.DrawLine(transform.position, hit.point, Color.red);
            dir += hit.normal * 50;
    }

}

var leftR = transform.position;
var rightR = transform.position;

leftR.x -= 2;
rightR.x += 2;

if(Physics.Raycast(leftR, transform.forward, hit, 20)){
    if(hit.transform != transform){
        Debug.DrawLine(leftR, hit.point, Color.red);
        dir += hit.normal * 50;
    }
}
if(Physics.Raycast(rightR, transform.forward, hit, 20)){
    if(hit.transform != transform){
        Debug.DrawLine(rightR, hit.point, Color.red);
        dir += hit.normal * 50;
    }
}

var rot = Quaternion.LookRotation(dir);

var sizeOfMonster = 1.5;
var groundOffset = Vector3.up;
var newPosition = transform.position + transform.forward * 1 * Time.deltaTime;
if (!Physics.CheckSphere(newPosition+groundOffset, sizeOfMonster)){
transform.position = newPosition;
}

transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
transform.position += transform.forward * 1 * Time.deltaTime;

}

I'm going to go out on a limb and ask if the "Monster Size" determines if they can pass through a wall or not, is that correct?

EDIT here is an image of how things are set up and hopefully it demostrates what my Alien Zombies are doing wrong. I added the debug code so I was much better able to adjust the parameters and center the sphere but as you can see they still just waltz right through my structures?? alt text

It's not easy to write a good AI code. But often a simple AI script is good enough as the one you have.

I suggest one simple addition to your script:

Only update the position to the new value if it doesn't collide with anything (Use Physics.CheckSphere(...) to check for a potential collision).

In code it would be something like (sizeOfMonster and groundOffset should be replaced with variables for your game):

var sizeOfMonster = 1.0;
var groundOffset = Vector3.up;  
var newPosition = transform.position + transform.forward * 1 * Time.deltaTime;
if (!Physics.CheckSphere(newPosition+groundOffset, sizeOfMonster)){
transform.position = newPosition;
}

If you want more advanced AI you should take a look at Path or Behave ( http://angryant.com/ ). Or take a look at my blog about A* path finding ( http://blog.nobel-joergensen.com/2011/02/26/a-path-finding-algorithm-in-unity/ ).

Edited 28th of May: To help debugging the size and position of the sphere use the following code snippet. This will draw the sphere-test collider in the scene when the GameObject is selected in the navigator:

function OnDrawGizmosSelected () {
    Gizmos.color = Color.red;
    var sizeOfMonster = 1.0;
    var groundOffset = Vector3(0,0.5,0); 
    Gizmos.DrawWireSphere (transform.position+groundOffset, sizeOfMonster);
}