- Home /
How do I keep the enemies from moving to waypoints in other rooms?
Hello, I'm using waypoints to have the enemies patrolling the dungeon rooms they are in. Currently though If I put waypoints in a separate room then all the enemies register those waypoints as well and try adding them to their patrol zone...so currently everyone is moving against a brick wall...literally. How would I go about having them register only waypoints in a given radius of the enemy? I'm assuming something with Random.Range but I have no idea how I'd write that or where I'd at that. Below is the script I'm using for the enemy AI.
namespace UnityStandardAssets.Characters.ThirdPerson { public class EnemySight : MonoBehaviour { [SerializeField] Animator enemyAnim;
public NavMeshAgent agent;
public ThirdPersonCharacter character;
//Anim bools
private bool IsWalking;
private bool IsDead;
private bool IsAttacking;
//enemy health
public float eLife;
public enum State
{
PATROL,
CHASE,
INVESTIGATE
}
public State state;
private bool alive;
//Variables for patrolling
public GameObject[] waypoints;
private int waypointInd;
public float patrolSpeed = 0.5f;
//Variables for Chasing
public float chaseSpeed = 1f;
public GameObject target;
//Variables for Investigating
private Vector3 investigateSpot;
private float timer = 0;
public float investigateWait = 10;
//Variables for Sight
public float heightMultiplier;
public float sightDist = 10;
private void Start()
{
//sets up enemy AI states
agent = GetComponent<NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
agent.updatePosition = true;
agent.updateRotation = false;
//sets up waypoints
waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
waypointInd = Random.Range(0, waypoints.Length);
state = EnemySight.State.PATROL;
alive = true;
//anim bools for enemy
enemyAnim.SetBool("IsWalking", false);
enemyAnim.SetBool("IsDead", false);
enemyAnim.SetBool("IsAttacking", false);
//sets up state machine
StartCoroutine(FSM());
}
private void Update()
{
if (eLife == 0)
{
eDeath();
}
}
IEnumerator FSM()
{
while (alive)
{
switch (state)
{
case State.PATROL:
Patrol();
break;
case State.CHASE:
Chase();
break;
case State.INVESTIGATE:
Investigate();
break;
}
yield return null;
}
}
void Patrol()
{
agent.speed = patrolSpeed;
if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2)
{
agent.SetDestination(waypoints[waypointInd].transform.position);
character.Move(agent.desiredVelocity, false, false);
}
else if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) <= 2)
{
waypointInd = Random.Range(0, waypoints.Length);
}
else
{
character.Move(Vector3.zero, false, false);
}
enemyAnim.SetBool("IsWalking", true);
}
void Chase()
{
agent.speed = chaseSpeed;
agent.SetDestination(target.transform.position);
character.Move(agent.desiredVelocity, false, false);
}
void Investigate()
{
timer += Time.deltaTime;
RaycastHit hit;
Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, transform.forward * sightDist, Color.green);
Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, (transform.forward + transform.right).normalized * sightDist, Color.green);
Debug.DrawRay(transform.position + Vector3.up * heightMultiplier, (transform.forward - transform.right).normalized * sightDist, Color.green);
if (Physics.Raycast(transform.position + Vector3.up * heightMultiplier, transform.forward, out hit, sightDist))
{
if (hit.collider.gameObject.tag == "Player")
{
state = EnemySight.State.CHASE;
target = hit.collider.gameObject;
}
}
if (Physics.Raycast(transform.position + Vector3.up * heightMultiplier, (transform.forward + transform.right).normalized, out hit, sightDist))
{
if (hit.collider.gameObject.tag == "Player")
{
state = EnemySight.State.CHASE;
target = hit.collider.gameObject;
}
}
if (Physics.Raycast(transform.position + Vector3.up * heightMultiplier, (transform.forward - transform.right).normalized, out hit, sightDist))
{
if (hit.collider.gameObject.tag == "Player")
{
state = EnemySight.State.CHASE;
target = hit.collider.gameObject;
}
}
agent.SetDestination(this.transform.position);
character.Move(Vector3.zero, false, false);
transform.LookAt(investigateSpot);
if (timer >= investigateWait)
{
state = EnemySight.State.PATROL;
timer = 0;
}
}
void OnTriggerEnter(Collider coll)
{
if (coll.tag == "Player")
{
state = EnemySight.State.INVESTIGATE;
investigateSpot = coll.gameObject.transform.position;
}
}
public void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Fire")//damages enemy
{
eLife--;
}
//calls attack animation
if (other.gameObject.tag == "Player")
{
enemyAnim.SetBool("IsWalking", false);
enemyAnim.SetBool("IsAttacking", true);
}
}
public void OnCollisionExit(Collision other)
{
//stops attack animation adn starts walking animation
if (other.gameObject.tag == "Player")
{
enemyAnim.SetBool("IsAttacking", false);
enemyAnim.SetBool("IsWalking", true);
}
}
void eDeath()
{
enemyAnim.SetBool("IsWalking", false);
enemyAnim.SetBool("IsDead", true);
//enemyAnim.Play("Dead");
Destroy(this.gameObject, 3.5f);
}
}
Answer by ShadyProductions · Mar 11, 2020 at 01:27 PM
What you actually should be doing is keeping a list of waypoints per room, and assign these to the enemies based on, in which room the enemy is. that way you shouldn't be having these kinds of issues.
So...an Index? how would I separate the index per room though? Or are am I overthinking and you mean to just manually add the waypoints to the enemies ins$$anonymous$$d of them automatically detecting them?
$$anonymous$$ake a class for your rooms that have waypoints specific to that room defined in them:
public class Room
{
public List<Vector3> Waypoints;
}
Assu$$anonymous$$g you have then somewhere a list of rooms and a method for spawning your enemies per room you can do something like this:
public class Dungeon
{
public List<Room> Rooms;
public void SpawnEnemies()
{
foreach (var room in Rooms)
{
// Assu$$anonymous$$g you have a class Enemy with waypoints
var enemy = Instantiate<Enemy>(enemyPrefab, position, Quaternion.identity);
enemy.Waypoints = room.Waypoints; // Assign room's waypoints to the enemy
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Looping Between Waypoints 1 Answer
Enemy stopped at first waypoints. 1 Answer
Enemy Combat AI 1 Answer