- Home /
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);
}
First of all you have some illogical things here in your code: - Awake will be called only once, you shouldn't put something whose value changes at every frame (Time.deltaTime) - Update is read at each frame, and you put something static in it, once you told the coordinates to the computer, it will remember it, you don't need to tell him back everytime.
Hence, you should but what is in your Awake, in your Update and what is in your Update in your Start (or Awake, but Start is enough here)
Ins$$anonymous$$d of invoke you can also make a timer (if you don't know how, there are a lot of answers here on Unity Answers), when you reach your timer, you set a bool to true. And in Update you add an if statement: if my bool about retreat i true, call the bunyipRetreat() function.
Answer by DwaynePritchett · Apr 21, 2015 at 06:21 AM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613193553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy rotation issues - Javascript 0 Answers
get closest enemy 1 Answer
enemy respawn after amount of time 2 Answers
Throw the Player from the Enemy away 1 Answer
Help with Enemy AI 1 Answer