hi guy,
i just wanted to show you my solution and ask if this will cause any problems in later sections, because i called navmeshagent here in AIController.cs? I thought the only guys using the patrol system will be the enemies and at the moment there is no need for max speed because everything is set manually.
quick overview:
- created 2 speeds
- in start patrolSpeed is set via an extra function (SetNavMeshSpeed)
- if chasing -> chasespeed
- right before patrol patrolSpeed is set
Very Simple and working behaviour (at the moment)
What do you think?
namespace RPG.Control
{
public class AIController : MonoBehaviour
{
[SerializeField] float chaseDistance = 5f;
[Range(0, 10)]
[SerializeField] float patrolSpeed = 3f;
[Range(0, 10)]
[SerializeField] float chaseSpeed = 5.6f;
[SerializeField] float suspicionTime = 3f;
[SerializeField] PatrolPath patrolPath;
[SerializeField] float waypointTolerance = 1f;
[SerializeField] float waypointDwellTime = 3f;
Fighter fighter;
Health health;
Mover mover;
GameObject player;
Vector3 guardPosition;
float timeSinceLastSawPlayer = Mathf.Infinity;
float timeSinceArrivedAtWaypoint = Mathf.Infinity;
int currentWaypointIndex = 0;
private void Start()
{
fighter = GetComponent<Fighter>();
health = GetComponent<Health>();
mover = GetComponent<Mover>();
player = GameObject.FindWithTag("Player");
SetNavMeshSpeed(patrolSpeed);
guardPosition = transform.position;
}
private void Update()
{
if (health.IsDead()) return;
if (InAttackRangeOfPlayer() && fighter.CanAttack(player))
{
SetNavMeshSpeed(chaseSpeed);
AttackBehaviour();
}
else if (timeSinceLastSawPlayer < suspicionTime)
{
SuspicionBehaviour();
}
else
{
SetNavMeshSpeed(patrolSpeed);
PatrolBehaviour();
}
UpdateTimers();
}
public float SetNavMeshSpeed(float speed)
{
return GetComponent<NavMeshAgent>().speed = speed;
}
private void UpdateTimers()
{
timeSinceLastSawPlayer += Time.deltaTime;
timeSinceArrivedAtWaypoint += Time.deltaTime;
}
private void PatrolBehaviour()
{
Vector3 nextPosition = guardPosition;
if (patrolPath != null)
{
if (AtWaypoint())
{
timeSinceArrivedAtWaypoint = 0;
CycleWaypoint();
}
nextPosition = GetCurrentWaypoint();
}
if (timeSinceArrivedAtWaypoint > waypointDwellTime)
{
mover.StartMoveAction(nextPosition);
}
}
private Vector3 GetCurrentWaypoint()
{
return patrolPath.GetWaypoint(currentWaypointIndex);
}
private void CycleWaypoint()
{
currentWaypointIndex = patrolPath.GetNextIndex(currentWaypointIndex);
}
private bool AtWaypoint()
{
float distanceToWaypoint = Vector3.Distance(transform.position, GetCurrentWaypoint());
return distanceToWaypoint < waypointTolerance;
}
private void SuspicionBehaviour()
{
GetComponent<ActionScheduler>().CancelCurrentAction();
}
private void AttackBehaviour()
{
timeSinceLastSawPlayer = 0;
fighter.Attack(player);
}
private bool InAttackRangeOfPlayer()
{
float distanceToPlayer = Vector3.Distance(player.transform.position, transform.position);
return distanceToPlayer < chaseDistance;
}
// Called by Unity
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, chaseDistance);
}
}
}
I would be gratefl for any kind of tips. I will follow the code changes in the videos and just want to know what you think about my solution. Thank you