# Making Enemy Retreat

Hello! I’m trying to make an enemy retreat after so many seconds after it’s spawned. I tested out the transform.position and the enemy moves straight towards, so I know that part works. However, when I tried to make the time delay happen by using invoke() code and putting the transform code in a custom function, the enemy remains where it was spawned.

`````` #pragma strict

public var enemyspeed : float; //how fast enemy goes
private var retreatPoint: Vector3; //holds the coordinates to the retreat point
private var step : float; //speed *frames value

function Awake () {
// sets speed of enemy
var step = enemyspeed * Time.deltaTime;
}

function Start () {
//after 4 seconds, bunyip retreats
Invoke ("BunyipRetreat", 4);
}

function Update () {
//coordinates for retreatingpoint
retreatPoint = Vector3(24,0,20);

}

//custom function
function BunyipRetreat() {
//moves bunyip to retreating point
transform.position = Vector3.MoveTowards(transform.position,retreatPoint,step);
}
``````

I think MoveTowards needs to be in the Update function. Try this.

`````` #pragma strict

public var enemyspeed : float; //how fast enemy goes
private var retreatPoint: Vector3; //holds the coordinates to the retreat point
private var step : float; //speed *frames value
public var retreating : bool; //A trigger to see if retreating is true

function Awake () {
// sets speed of enemy
var step = enemyspeed * Time.deltaTime;
}

function Start () {
//after 4 seconds, bunyip retreats
Invoke ("BunyipRetreat", 4);
}

function Update () {
//coordinates for retreatingpoint
retreatPoint = Vector3(24,0,20);
if(retreating){
transform.position = Vector3.MoveTowards(transform.position,retreatPoint,step);
}
}

//custom function
function BunyipRetreat() {
retreating = true;

}
``````

Note* I do not use UnityScript/JavaScript, so that might be broken terribly. But, I hope it gives you an idea of what you need to do.

-Dwayne Pritchett