Wandering AI Monster

Hi, I'm making a simple hunting game with an alien theme. The story is you're on an island in an alien world, and you have to hunt to survive. I have a weird alien dog-like monster that has a walk animation. I want it to walk around randomly until it comes close to the player or vice-versa. When that happens I want it to run away until it's twice as far as when it first detects the player. I have no idea how to do this, can anyone help?

Hi there,

Assuming you are making a 3D Game, for the solution to this, I strongly recommend the 3rd person Platformer tutorial provided on the Unity site. It can be found here:

http://unity3d.com/support/resources/tutorials/3d-platform-game.html

If you are well-versed in scripting and the Unity engine, I would go ahead and skip to the chapter on the enemy AI (the chapter titled "Adverseries" Page 69 in my Reader). The robots behave in a manner similar to what you are describing.

If you are completely new to Unity - definitely try some of the easier tutorials, 3D Platformer assumes you are already fairly well-versed in Scripting and the Unity platform.

I made it so if I go close to it, it walks away. I still want it to roam around when I'm not close and it also has a little glitch. If I'm above it on a hill or jumping while I'm close enough, it starts to go down under the terrain. Also if I'm really close to it, it sometimes starts walking in midair. Here's my code:

var person : Transform; var detected : boolean = false; function Update () {

if(Vector3.Distance(person.position,transform.position) <= 30)
{
    detected = true;
}
else
{
    detected = false;
}

if(detected)
{
    var relativePos = person.position - transform.position;
    relativePos = relativePos * -1;
    var rotation = Quaternion.LookRotation(relativePos);

    transform.rotation = rotation;
    animation.CrossFade("walk");
    transform.Translate(Vector3.forward * (Time.deltaTime * 2));
}
else
{
    animation.CrossFade("idle");
}

}

Can someone please help?