Find enemy problem.

Hey, I have two boxes trying to get one to chase the other, but it wont move :/

This is the code:

var enemy: Transform;
var player: GameObject;
var dir: Vector3;
var speed: float;
var observed: boolean;

function update () {
if(observed){
dir = player.transform.position - transform.position;
dir = dir.normalized;

transform.Translate(dir * speed, Space.World);
}
else {
    transform.eulerAngles.y = Mathf.PingPong(Time.time*20,90) -45;

}

}

function OnTriggerEnter (other:Collider) {
    if(player) observed =true;
}

put a debug log in each part of the function and find out exactly where the script gets to. Post back here with the results and then you will definatly be closer to getting your question answered.

1 Answer

1

Well, for one thing, you need to multiply your speed by Time.deltaTime. But it seems like you're making things hard on yourself. Why not give this a try:

function Update()
{
if (observed)
{
transform.LookAt(enemy);
transform.Translate(transform.forward*speed*Time.deltaTime);
}
}