- Home /
Need help with enemy stop and go movement
I've been wrestling with this for a while now, and something just isn't clicking for me. I'm working on developing some behaviors for some enemies in a top-down 2D RPG. Right now I'm trying to create a script that will have enemies move in a random direction for a certain duration, stop for a certain duration, then pick a new random direction, move, then stop, then move, etc. I'm not sure what to do here. I've been trying to get two timers to work together with no success. I might be missing something simple, but for whatever reason, I'm not seeing it. Getting the enemies to move and change direction isn't the hard part. I found some code online that helps with that (although I might change it to move just left, right, up, and down). I just can't figure out how to get a pause to work. I've tried using other timers and even tried a coroutine without success. As a starting point, here is the code I found online. Any help will be GREATLY appreciated. I get the feeling I might just need a nudge to get everything to fall into place in my head.
private float latestDirectionChangeTime;
private readonly float directionChangeTime = 3f;
private float characterVelocity = 2f;
private Vector2 movementDirection;
private Vector2 movementPerSecond;
void Start()
{
latestDirectionChangeTime = 0f;
calcuateNewMovementVector();
}
void calcuateNewMovementVector()
{
//create a random direction vector with the magnitude of 1, later multiply it with the velocity of the enemy
movementDirection = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized;
movementPerSecond = movementDirection * characterVelocity;
}
void Update()
{
//if the changeTime was reached, calculate a new movement vector
if (Time.time - latestDirectionChangeTime > directionChangeTime)
{
latestDirectionChangeTime = Time.time;
calcuateNewMovementVector();
}
//move enemy:
transform.position = new Vector2(transform.position.x + (movementPerSecond.x * Time.deltaTime),
transform.position.y + (movementPerSecond.y * Time.deltaTime));
}
Answer by Tsaras · Feb 25, 2019 at 08:38 PM
You can use a boolean flag to set this up. The code might be something like this (not tested):
float moveDuration;
float pauseDuration;
float timer;
bool paused;
void Start(){
timer = moveDuration; //to get it moving from the start
paused = false;
}
void Update(){
timer -= Time.deltaTime;
if (timer <=0){
if (paused){
timer = moveDuration;
FindNewDirection();
paused = false;
} else {
timer = pauseDuration;
paused = true;
}
return;
}
if (!paused){
//move character here
}
}
I got your code to work. Thank you! I'm going to study it so I can better understand it and not just use it. Doing this sort of thing has always been an obstacle for me and I need to get past it. Here is my implementation (although I will most likely tweak it as necessary once I understand it better). Again, thank you!
public float moveDuration = 1f;
public float pauseDuration = 2f;
private float timer;
private bool paused;
private Vector2 movementDirection;
private Vector2 movementPerSecond;
private float characterVelocity = 2f;
protected override void Start()
{
timer = moveDuration; //to get it moving from the start
paused = false;
}
protected override void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
if (paused)
{
timer = moveDuration;
CalcuateNew$$anonymous$$ovementVector();
paused = false;
}
else
{
timer = pauseDuration;
paused = true;
}
return;
}
if (!paused)
{
transform.position = new Vector2(transform.position.x + (movementPerSecond.x * Time.deltaTime),
transform.position.y + (movementPerSecond.y * Time.deltaTime));
}
}
void CalcuateNew$$anonymous$$ovementVector()
{
//create a random direction vector with the magnitude of 1, later multiply it with the velocity of the enemy
movementDirection = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized;
movementPerSecond = movementDirection * characterVelocity;
}
Glad I helped. The basis is the paused flag toggling between true and false to change between the move and pause states of the enemy when the timer has counted down the appropriate time. Then only when paused is false it moves. You can even remove the return; statement.
Answer by xxmariofer · Feb 25, 2019 at 08:30 PM
well here is the easiest way to achieve this without touching much of the code, override the start and update method and add theInvokIsWaiting one.
private bool isWaiting;
void Start()
{
isWaiting = false;
latestDirectionChangeTime = 0f;
calcuateNewMovementVector();
}
void InvokeIsWaiting()
{
latestDirectionChangeTime = Time.time;
isWaiting = false;
}
void Update()
{
//if the changeTime was reached, calculate a new movement vector
if (Time.time - latestDirectionChangeTime > directionChangeTime && !isWaiting)
{
isWaiting = true;
calcuateNewMovementVector();
Invoke("InvokeIsWaiting", 4) //will wait for 4 seconds this is just an example use your desired value
}
//move enemy:
transform.position = new Vector2(transform.position.x + (movementPerSecond.x * Time.deltaTime),
transform.position.y + (movementPerSecond.y * Time.deltaTime));
}
I tried this, but couldn't get it to work. The enemy just changes direction after a certain duration without a pause in between. Any ideas what I did wrong?
public float waitTime = 2f;
public float directionChangeTime = 3f;
private float latestDirectionChangeTime;
private float characterVelocity = 2f;
private Vector2 movementDirection;
private Vector2 movementPerSecond;
private bool isWaiting;
protected override void Start()
{
isWaiting = false;
latestDirectionChangeTime = 0f;
CalcuateNew$$anonymous$$ovementVector();
}
protected override void Update()
{
//if the changeTime was reached, calculate a new movement vector
if (Time.time - latestDirectionChangeTime > directionChangeTime && !isWaiting)
{
isWaiting = true;
CalcuateNew$$anonymous$$ovementVector();
Invoke("InvokeIsWaiting", waitTime);
}
//move enemy:
transform.position = new Vector2(transform.position.x + (movementPerSecond.x * Time.deltaTime),
transform.position.y + (movementPerSecond.y * Time.deltaTime));
}
void InvokeIsWaiting()
{
latestDirectionChangeTime = Time.time;
isWaiting = false;
}
void CalcuateNew$$anonymous$$ovementVector()
{
//create a random direction vector with the magnitude of 1, later multiply it with the velocity of the enemy
movementDirection = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized;
movementPerSecond = movementDirection * characterVelocity;
}
i did it fast and couldnt test it, i know you already found a solution but here is a tested working one
public float waitTime = 2f;
public float directionChangeTime = 3f;
private float latestDirectionChangeTime;
private float characterVelocity = 2f;
private Vector2 movementDirection;
private Vector2 movementPerSecond;
private bool isWaiting;
protected void Start()
{
isWaiting = false;
latestDirectionChangeTime = 0f;
CalcuateNew$$anonymous$$ovementVector();
}
protected void Update()
{
//if the changeTime was reached, calculate a new movement vector
if (Time.time - latestDirectionChangeTime > directionChangeTime && !isWaiting)
{
isWaiting = true;
Invoke("InvokeIsWaiting", waitTime);
movementPerSecond = Vector3.zero;
}
//move enemy:
transform.position = new Vector2(transform.position.x + (movementPerSecond.x * Time.deltaTime),
transform.position.y + (movementPerSecond.y * Time.deltaTime));
}
void InvokeIsWaiting()
{
latestDirectionChangeTime = Time.time;
CalcuateNew$$anonymous$$ovementVector();
isWaiting = false;
}
void CalcuateNew$$anonymous$$ovementVector()
{
//create a random direction vector with the magnitude of 1, later multiply it with the velocity of the enemy
movementDirection = new Vector2(UnityEngine.Random.Range(-1.0f, 1.0f), UnityEngine.Random.Range(-1.0f, 1.0f)).normalized;
movementPerSecond = movementDirection * characterVelocity;
}
I got yours to work, too :). Thank you! Going to post another question about keeping these randomly moving enemies confined to a specific area if you are interested :).