How would I make an enemy follow me?

Sorry, I made this thread in Unity support, then I realised it goes in scripting…

I have an enemy with 2 animations:
Walk
and bite

I want the enemy to walk towards you, and then bite you, causing you to lose 1 HP.

How would I script this?

I’m no scripting expert but I found the Lerpz 3rd Person Platform tutorial on the main support page very helpful, and it has an entire section on this. If you haven’t already, you should check it out, there is a script in the tutorial file that you apply which causes an enemy to follow you.

Lol, Actually I am trying to get the character named Byte from the 3D platform tutorial to follow you…

Which script again?

Hi!

////////////////////// SCRIPT TO THE ENEMY//////// enemy_follower.js

var target : Transform;
var vel_vec : Vector3;
var speed : float;
var distance_byte : float;
var player_life : Player_life_script;

function Update()
{
vel_vec = target.position - transform.position; // creates the vector from the enemy to you.
transform.LookAt(target); // enemy looks to you
transform.Translate(Vector3.forward * speed * Time.deltaTime); // enemy walks to you
if (vel_vec.magnitude <= distance_byte) //if is near
{
animation.Play(“byte”, PlayMode.StopAll);
player_life.hp -= 1;
vel_vec = vector.zero;
}
else if (vel_vec.magnitude > distance_byte + 5) // if is far
animation.Play(“walk”, PlayMode.StopAll);
}

////////////////////// OTHER SCRIPT TO THE PLAYER //////// player_life_script.js

var hp : float;

function Update() {

if (hp <= 0)
Destroy.GameObject;

}

Tell me your questions

The script in the tutorial is titled: “Enemy Police Guy”. When I added it, it worked immediately. All I had to do was drag it onto the AI. Make sure the AI has a character controller too, and that he isn’t stuck in the ground or something.

Edit: The character I am talking about, or at least the prefab name, is called “Copper”, not Byte.

I guess The Enemy Police Guy script does not work with the Bytes.

Also person who gave me those scripts:

I get errors.

which errors?

Player_life_script error:

Assets/Scripts/Player/Player_life_script.js(6,9): BCE0019: ‘GameObject’ is not a member of ‘function(UnityEngine.Object, float): void’.

enemy_follow error:

Assets/Scripts/Enemies/enemy_follow.js(16,11): BCE0005: Unknown identifier: ‘vector’.

////////////////////// SCRIPT TO THE ENEMY//////// enemy_follower.js

var target : Transform;
var vel_vec : Vector3;
var speed : float;
var distance_byte : float;
var player_life : Player_life_script;

function Update()
{
vel_vec = target.position - transform.position; // creates the vector from the enemy to you.
transform.LookAt(target); // enemy looks to you
transform.Translate(Vector3.forward * speed * Time.deltaTime); // enemy walks to you
if (vel_vec.magnitude <= distance_byte) //if is near
{
animation.Play(“byte”, PlayMode.StopAll);
player_life.hp -= 1;
vel_vec = Vector3.zero;
}
else if (vel_vec.magnitude > distance_byte + 5) // if is far
animation.Play(“walk”, PlayMode.StopAll);
}

////////////////////// OTHER SCRIPT TO THE PLAYER //////// player_life_script.js

var hp : float;

function Update() {

if (hp <= 0)
Destroy(gameObject);

}

try now

Thanks, but I need to PM you about something.