# Zombie Following

I have to make a zombie follow you in a game. Is there a script that make the zombie go after the person. I start of with a simple capsule but it will soon be a zombie.

Ugh, another making a zombie game. First, I reccomend looking at the FPS tutorial AI script. I tried making my own AI, and utterly failed. But if you insist:

``````var target : Transform;
var speed : float = 5;
function Update() {
transform.LookAt(target);
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
``````

Thats basicly it. If you want it to follow at a distance, use a raycast or vector3.distance and an if statement.

Thanks!

I found part of this on the forums and then modified it a bit. still needs improving but this should help you out.

``````static var player : GameObject;
var speed : float = 1;
var tplayer : Transform;

var interval = 1;
private var nextTurn = 0.0;
var rspeed:int;

function Start ()
{
player = GameObject.FindGameObjectWithTag("Player");

if (!player)
Debug.Log ("ERROR could not find Player!");

}

function Update()
{
if (!player)

return;

var distance = Vector3.Distance( player.transform.position, transform.position);
//if player is in distance within 30 units, run to him and look at him.
if ( distance < 30  )
{
Debug.Log ("player is close");

var delta = player.transform.position - transform.position;
delta.Normalize();

var moveSpeed = speed * Time.deltaTime;

transform.position = transform.position + (delta * moveSpeed);

transform.LookAt(tplayer);

}
else
{
//if player is not in range, wander around
Debug.Log("not close yet - wandering - " + distance);
if (Time.time > nextTurn) {
nextTurn = Time.time + (Random.value * interval);
// nextTurn = Time.time + interval;
transform.Rotate(0, Random.Range(-30, 30), 0);
}
transform.Translate(transform.forward * Time.smoothDeltaTime * rspeed);
}

}

function OnCollisionEnter(collision : Collision)
{

//this is when they are wandering, if they hit a wall they turn around in a random direction
if (collision.transform.name == "Wall")
{
transform.Rotate(0, 60, 0);
transform.Translate(transform.forward * Time.smoothDeltaTime * rspeed);
}
}
``````