An AI that will move away or move to another waypoint when player goes near?

I am doing this simple project of catching things and I want to have an AI on my project so it will be more challenging. Any tutorials or suggestion you might give me? I want my AI to avoid my player when it comes near. Thanks!

take look in this FPS have AutoWayPoint scrit for AI...

in that they just make some wayPoint and those Robot(Enemies) will move automatically towards to that waypoints...change your script as whatever you need...

find the distance between player and enemy....if it's very near you may move it....

I got this to work for one of my Alien Zombies that I needed to be a scared Alien Zombie. You'll need to change a few things and add the Waypoint functions but it makes a character run away if you get within a certain distance.

var modelAnimation : Animation;
var awareDistance : float = 25.0;
var scaredDistance : float = 20.0;
var player : Transform;

var runSpeed : float = 15.0;

enum AIStatus {idle = 0, Scared = 1}
private var status = AIStatus.idle;

var controller : CharacterController;

private var moveDirection =;

function Awake()
    controller = GetComponent(CharacterController);

function Update()

        case AIStatus.idle:

        case AIStatus.Scared:



function idle()
    modelAnimation.CrossFade ("idle");

function RunAway()
    transform.eulerAngles.y = player.transform.eulerAngles.y;
    moveDirection = Vector3(0,0,40);    
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= runSpeed;
    controller.SimpleMove(moveDirection * Time.deltaTime);

    modelAnimation.CrossFade ("RunAttack");


function CheckStatus()

    var dist = (player.position - transform.position).magnitude;

    if(dist < scaredDistance)
        status = AIStatus.Scared;

    else if (dist > awareDistance)
        status = AIStatus.idle;


@Michael Knight how would I combine that lovely script above with an array? The idea is to drop this scrip on a scrip manager ( empty game object) and with the array just assign what I want to run away from to the array list via the inspector.


I am new to unity & scripting & I just wanna say after hours/days of frustration that script was a big help for me to understand how to get some npc animal characters to act like AI animals. Good script, thanks for sharing it!