Suspicious Behaviour - Animated - My solution

I downloaded a simple LookAround animation from Mixamo and created a new State in the animator that is linked to the locomotion (since you can’t actually get directly to attack from suspicion) that has the same exit triggers as the Attack.
Here is how it looks like

I looked at how the code is implemented so far and simply duplicated the way “Fighter.cs” is set up. I created a new “Suspicion.cs” class and added it to the Enemy Prefab. I figured this way, if you want to add more functionality or VFX, this would offer more flexibility. Here is what it contains

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Movement;
using RPG.Core;

namespace RPG.Combat
{
    public class Suspicion : MonoBehaviour, IAction
    {
        Health target;

        public void TriggerSuspicion()
        {
            GetComponent<Animator>().ResetTrigger("stopLookAround");
            GetComponent<Animator>().SetTrigger("lookAround");
        }


        public void Cancel()
        {
            StopLooking();
            target = null;

        }

        private void StopLooking()
        {
            GetComponent<Animator>().SetTrigger("stopLookAround");
            GetComponent<Animator>().ResetTrigger("lookAround");
        }

    }

}

And in AIController I’ve set it up like this

        void GuardBehaviour()
        {
            suspicion.Cancel();
            mover.StartMoveAction(guardPosition);
            if (InGuardingRange())
            {
                transform.rotation = Quaternion.RotateTowards(transform.rotation, guardRotation, 5);
            }
        }

        void SuspicionBehaviour()
        {
            GetComponent<ActionScheduler>().CancelCurrentAction();
            guardDestination = navMeshAgent.destination;
            if (guardDestination == transform.position)
            {
                suspicion.TriggerSuspicion();
            }

        }

        void AttackBehaviour()
        {
            suspicion.Cancel();
            fighter.Attack(player);
        }

The guardDestination if statement is needed to trigger the animation AFTER it reaches the last destination. Without it, my animation was triggering while the agent was moving. By adding that check, it triggers correctly …as far as I know :slight_smile:

5 Likes

Nice, the guarddestination is what i was missing!
thanks

Privacy & Terms