OK - so this morning I decided to uncomment that line GetComponent< Mover>().Cancel(); and try again.
It worked as expected. I actually changed just one other thing - the suspicion time which was 2 seconds in my implementation I changed to the 4 seconds as per Sam’s video.
And then I figured it out.
Mover (as triggered by Fighter) keeps moving until it reaches its previously set destination. During this time the suspicion time clock keeps running. If it’s only 2 seconds, that time can be consumed during the last commanded move operation.
Honestly I struggled with this for hours. During the challenge phase I used " GetComponent< Fighter>().Cancel(); and couldn’t get it to work after an hour+. Then I watched the video and changed it to GetComponent< ActionScheduler>().CancelCurrentAction(); thinking that was the trick but again it didn’t work. I spent another hour++ And then I spent an hour+ this morning.
I’m not sure what the lesson learned here is other than it’s really important for the code to be debuggable. VERY weird stuff starts to happen when these time intervals start overlapping (e.g. suspicion time overlaps the time it takes for a character to walk). I think I much prefer state machines (or behavior trees which I am less experienced in) which seems far easier to debug.
Leaving this thread open for someone to comment if they have a good lesson learned out of all this.
EDIT: This also makes me want to implement my own logging class. This is what I did inside the AIController’s Update method to help catch this. A complete hack but it worked. I would love to have a reusable version of this. I’m thinking something more structured like a Behavior tree or FSM could offer this for free.
Again leaving this open for folks to comment with lessons learned I should take from this.
private void Update()
{
if (health.IsDead()) return;
if (InAttackRangeOfPlayer() && fighter.CanAttack(player))
{
timeSinceLastLogChaseState += Time.deltaTime;
if (timeSinceLastLogChaseState > logInterval)
{
Debug.Log($"Chase Mode");
timeSinceLastLogChaseState = 0;
}
timeSinceLastSawPlayer = 0;
AttackBehaviour();
}
else if (timeSinceLastSawPlayer < suspicionTime)
{
timeSinceLastLogSuspicionState += Time.deltaTime;
if (timeSinceLastLogSuspicionState > logInterval) {
Debug.Log($"Suspicion Mode with TimeSince {timeSinceLastSawPlayer} s and max time: {suspicionTime} s");
timeSinceLastLogSuspicionState = 0;
}
SuspicionBehaviour();
}
else
{
GuardBehaviour();
}
timeSinceLastSawPlayer += Time.deltaTime;
}