Hi there!
Im working on a game at the moment that uses a grid for movement. The trouble am having at the moment is with moving my enemies. I have it at the moment so that the enemies will move in the grid and will move in a random direction once it reaches a new grid. I am however after a way to make it so that it stays in the direction it picks, and keeps going for a random period of time (e.g. 2 - 4 seconds) and then it chooses another direction to go, but also if it it hits any objects then it will pick a new direction.
Here is the script that I am currently using. Any help would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyGridMove : MonoBehaviour
{
public float moveSpeed = 1f;
public Transform EnemyMovePoint;
public float RandomMoveX;
public float RandomMoveZ;
// Start is called before the first frame update
void Start()
{
EnemyMovePoint.parent = null;
}
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, EnemyMovePoint.position, moveSpeed * Time.deltaTime);
RaycastHit hit;
int RandomMoveX = Random.Range(-1, 2);
int RandomMoveZ = Random.Range(-1, 2);
if (Vector3.Distance(transform.position, EnemyMovePoint.position) <= 0.05f)
{
if (!Physics.SphereCast(transform.position, 0.5f, new Vector3(RandomMoveX, 0, RandomMoveZ), out hit, 1f))
{
EnemyMovePoint.position += new Vector3(RandomMoveX, 0f, RandomMoveZ);
}
}
}
}
Thanks!