Sorry if this is the wrong place to ask this question but I noticed a new bug while trying to solve a separate one (the latter being solved by adding a rigidbody to my character prefab).
The issue at hand is that when I click to move, all the enemies in the world want to move as well. My theory is that in the enemy movement logic, the enemies try to move towards where the player wants to move, but then get it cancelled because they are out of chase range.
Above you can see a gif of what I mean. Notice how in the scene view, enemies that are supposed to be patrolling quickly try to change what they are doing the second I click somewhere.
I suspect it might be in the enemy ai script, but I am not quite sure. It looks like this:
using RPG.Core;
using RPG.Movement;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Control
{
public class AIController : MonoBehaviour
{
[SerializeField] float chaseDistance = 5f;
[SerializeField] float suspicionnTime = 3f;
[SerializeField] PatrolPath path;
[SerializeField] float patrolSpeed;
[SerializeField] float runSpeed;
[SerializeField] float wayPointTolerance = 1f;
[SerializeField] float dwellTime = 3f;
GameObject player;
Health health;
Fighter fighter;
Mover mover;
Vector3 guardLocation;
float timeSinceLastSawPlayer = Mathf.Infinity;
float timeSinceArrivedAtWaypoint = Mathf.Infinity;
int currentWaypointIndex = 0;
private void Start()
{
player = GameObject.FindWithTag("Player");
fighter = GetComponent<Fighter>();
health = GetComponent<Health>();
guardLocation = transform.position;
mover = GetComponent<Mover>();
}
private void Update()
{
if (health.IsDead()) return;
if (InAttackRange() && fighter.CanAttack(player))
{
AttackingState();
}
else if (timeSinceLastSawPlayer < suspicionnTime)
{
SuspicionState();
}
else
{
PatrolBehaviour();
}
UpdateTimers();
}
private void UpdateTimers()
{
timeSinceLastSawPlayer += Time.deltaTime;
timeSinceArrivedAtWaypoint += Time.deltaTime;
}
private void PatrolBehaviour()
{
Vector3 nextPosition = guardLocation;
if (path != null)
{
if(AtWayPoint())
{
timeSinceArrivedAtWaypoint = 0f;
CycleWaypoint();
}
nextPosition = GetCurrentWayPoint();
}
if (timeSinceArrivedAtWaypoint > dwellTime)
{
mover.StartMoveAction(nextPosition);
mover.agentMover.speed = patrolSpeed;
}
}
private bool AtWayPoint()
{
float distanceToWaypoint = Vector3.Distance(transform.position, GetCurrentWayPoint());
return distanceToWaypoint < wayPointTolerance;
}
private Vector3 GetCurrentWayPoint()
{
return path.GetWayPoint(currentWaypointIndex);
}
private void CycleWaypoint()
{
currentWaypointIndex = path.GetNextIndex(currentWaypointIndex);
}
private void SuspicionState()
{
GetComponent<ActionScheduler>().CancelCurrentAction();
}
private void AttackingState()
{
timeSinceLastSawPlayer = 0f;
mover.agentMover.speed = runSpeed;
fighter.Attack(player);
}
private bool InAttackRange()
{
float distanceToPlayer = Vector3.Distance(player.transform.position, transform.position);
return distanceToPlayer < chaseDistance;
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, chaseDistance);
}
}
}
Thank you for any help.