Hi there,
For some reason, after my cutscene, the enemies seem to stop patrolling at all. Before the cutscene, they are patrolling (I checked in scene view), but after that, they don’t move unless I get into their chase radius.
Here is my AI Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Combat;
using RPG.Core;
using RPG.Movement;
using System;
using RPG.Attributes;
using GameDevTV.Utils;
namespace RPG.Control
{
public class AIController : MonoBehaviour
{
[SerializeField] float chaseDistance = 5f;
[SerializeField] float suspicionTime = 5f;
[SerializeField] PatrolPath patrolPath;
[SerializeField] float waypointTolerance = 1f;
[SerializeField] float waypointDwellTime = 3f;
[Range(0,1)]
[SerializeField] float patrolSpeedFraction = 0.2f;
Fighter fighter;
Health health;
GameObject player;
Mover mover;
LazyValue<Vector3> guardPosition;
float timeSinceLastSawPlayer = Mathf.Infinity;
float timeSinceArrivedAtWaypoint = Mathf.Infinity;
int currentWaypointIndex = 0;
private void Awake()
{
fighter = GetComponent<Fighter>();
health = GetComponent<Health>();
player = GameObject.FindWithTag("Player");
mover = GetComponent<Mover>();
guardPosition = new LazyValue<Vector3>(GetGuardPosition);
}
private void Start()
{
guardPosition.ForceInit();
}
private Vector3 GetGuardPosition()
{
return transform.position;
}
private void Update()
{
if (health.IsDead()) return;
if (InAttackRangeOfPlayer() && fighter.CanAttack(player))
{
AttackBehavior();
}
else if (timeSinceLastSawPlayer < suspicionTime)
{
SuspicionBehavior();
}
else
{
PatrolBehavior();
}
UpdateTimers();
}
private void UpdateTimers()
{
timeSinceLastSawPlayer += Time.deltaTime;
timeSinceArrivedAtWaypoint += Time.deltaTime;
}
private void PatrolBehavior()
{
Vector3 nextPosition = guardPosition.value;
if(patrolPath != null)
{
if(AtWaypoint())
{
timeSinceArrivedAtWaypoint = 0;
CycleWaypoint();
}
nextPosition = GetCurrentWayPoint();
}
if (timeSinceArrivedAtWaypoint > waypointDwellTime)
{
mover.StartMoveAction(nextPosition, patrolSpeedFraction);
}
}
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 SuspicionBehavior()
{
GetComponent<ActionScheduler>().CancelCurrentAction();
}
private void AttackBehavior()
{
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);
}
}
}
Cinematic Trigger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
namespace RPG.Cinematics
{
public class CinematicTrigger : MonoBehaviour
{
bool alreadyTriggered = false;
private void OnTriggerEnter(Collider other)
{
if(!alreadyTriggered && other.gameObject.tag == "Player")
{
alreadyTriggered = true;
GetComponent<PlayableDirector>().Play();
}
}
}
}
and Cinematic Control Remover:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using RPG.Core;
using RPG.Control;
namespace RPG.Cinematics
{
public class CinematicsControlRemover : MonoBehaviour
{
GameObject player;
private void Awake()
{
player = GameObject.FindWithTag("Player");
}
private void OnEnable()
{
GetComponent<PlayableDirector>().played += DisableControl;
GetComponent<PlayableDirector>().stopped += EnableControl;
}
private void OnDisable()
{
GetComponent<PlayableDirector>().played -= DisableControl;
GetComponent<PlayableDirector>().stopped -= EnableControl;
}
void DisableControl(PlayableDirector pd)
{
player.GetComponent<ActionScheduler>().CancelCurrentAction();
player.GetComponent<PlayerController>().enabled = false;
}
void EnableControl(PlayableDirector pd)
{
player.GetComponent<PlayerController>().enabled = true;
}
}
}
Thanks!