# How can I get this object to move in random directions?

Hey there, I want my enemy to rotate to a random direction, then I want it to move in that direction for a second, wait one second, then repeat the process. I can’t get this to work right sadly. The monster teleports in the direction it’s looking, which is a start. But i’d like it to actually move. Preferably with a speed.

``````        private void Start()
{
//_enemyStats = gameObject.GetComponent<EnemyStats>();
StartCoroutine(Move());
}

private IEnumerator Move()
{
while (true)
{
yield return new WaitForSeconds(1);
gameObject.transform.rotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
yield return new WaitForSeconds(1);
gameObject.transform.Translate(Vector3.forward * Time.deltaTime * 50);
print("IsMoving");
}
}
``````
``````        private void Start()
{
//_enemyStats = gameObject.GetComponent<EnemyStats>();
StartCoroutine(Move());
}

private IEnumerator Move()
{
while (true)
{
yield return new WaitForSeconds(1);
gameObject.transform.rotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
yield return new WaitForSeconds(1);
float moveTimer =1;
while (timer > 0){
gameObject.transform.Translate(Vector3.forward * Time.deltaTime * 50);
print("IsMoving");
moverTimer -= Time.deltaTime;
yield return null;
}
}
}
``````
4 Likes

I would do something like that. speed has maybe be adjusted.
Code Example

``````        private float timer = 0;
private bool isMoving = false;
private bool allowedMovement = false;
private float timerEndInSeconds = 1f;
private float speed = 50f;

void Start() {
allowedMovement = true;
}

void Update() {
Move();
}

private void Move() {
if (allowedMovement) {
if (!isMoving) {
timer = 0;
gameObject.transform.rotation = Quaternion.Euler(0, Random.Range(0f, 360f), 0);
isMoving = true;
} else {
timer += Time.deltaTime;
if (timer >= timerEndInSeconds) {
isMoving = false;
timer = 0;
} else {
gameObject.transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
}
}
}
``````

WaitForSeconds, the code waits at this point so what your code will do is

1. Wait one second
2. set random rotation
3. wait one second
4. translate ones to target point in one frame
5. and than again 1.

so in end you will not have an 1 second movement.

1 Like

Thank you so much guys, it worked! I wasn’t sure how to set up a timer (or how coroutines work for that matter), but this has done the trick, the enemy moves exactly as intended. And yea, 50 needs to be adjusted, I just made it a ridiculous number at the time to see if anything happens. I replaced 50 now with the monster’s actual speed stat.

I also update the script so that enemies actually chase the player if he gets too close.

New and improved code for anyone curious:

``````using System.Collections;
using UnityEngine;

namespace Assets.Scripts
{
public class EnemyDefaultMovement : MonoBehaviour
{
private EnemyStats _enemyStats;
private Transform _playerTransform;
public float AgroDistance;
public float Distance;

private void Start()
{
_enemyStats = gameObject.GetComponent<EnemyStats>();
_playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
StartCoroutine(Move());
}

private void Update()
{
_playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
Distance = Vector3.Distance(gameObject.transform.position, _playerTransform.position);
}

private IEnumerator Move()
{
while (true)
{
if (Vector3.Distance(gameObject.transform.position, _playerTransform.position) > _enemyStats.AgroDistance)
{
yield return new WaitForSeconds(1);
gameObject.transform.rotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
yield return new WaitForSeconds(1);
var moveTimer = .5f;
while (moveTimer > 0)
{
gameObject.transform.Translate(Vector3.forward * Time.deltaTime * _enemyStats.Speed);
moveTimer -= Time.deltaTime;
yield return null;
}
}
else if (Vector3.Distance(gameObject.transform.position, _playerTransform.position) < _enemyStats.AgroDistance)
{
yield return new WaitForSeconds(1);
var chaseTimer = Random.value;
while (chaseTimer > 0)
{
gameObject.transform.LookAt(_playerTransform, Vector3.up);
gameObject.transform.Translate(Vector3.forward * Time.deltaTime * _enemyStats.Speed);
chaseTimer -= Time.deltaTime;
yield return null;
}
}
}
}
}
}
``````
1 Like