Adding animations to suspicion behavior

has anyone added any animations to their suspicion behavior in the RPG combat series? I’m trying but can seem to get it right? when i set a trigger or use a bool it seems to be constantly entering the state but then full plays the animation after the suspicion time is over. perhaps I’m putting the logic in the wrong spot any help or insight as to how it can be done would be great gonna keep on with the course till then

Sorry I missed this one, Dustin.

First thing to check is to make sure that your transition to the “Suspicion” animation has “has Exit time” unchecked, and that the transition back to locomotion has “has Exit time” checked.

Beyond that, post your SuspicionBehavior method (in AIController) and we’ll take a look.

Hello Thanks for the replay sorry for my late response. I moved on a bit in the course in figured some of it out on my own still having issues perhaps you could help with. I have my suspicion animations set up like attack to enter the transition with a trigger then two transitions to enter back to locomotion one to play normal and one to cancel with movement. I got the animation to play at the correct time when the enemy loses track of the the player but for an unknown reason it prematurely triggers right before it does so. it also triggers for an unknown reason on the way back to the waypoint. I also have it set to play the suspicion animation while at waypoints but for some reason at random times at waypoints it cancels the animations for no reason

Heres my ai controller script the relevant parts

private void Update()

    {

        if (health.IsDead()) { return; }

        if (InAttackRangeOfPlayer() && fighter.CanAttack(player))

        {

            AttackBehavior();

        }

        else if (timeSinceLastSawPlayer < suspicionTime)

        {

            SuspicionBehavior();

            SuspicionTrigger();

        }

        else

        {

            PatrolBehavior();

        }

        UpdateTimers();

    }

    private void SuspicionTrigger()

    {

        GetComponent<Animator>().ResetTrigger("suspicion");

        GetComponent<Animator>().SetTrigger("suspicion");

    }

    private void UpdateTimers()

    {

        timeSinceLastSawPlayer += Time.deltaTime;

        timeSinceArrivedAtWaypoint += Time.deltaTime;

    }

    private void PatrolBehavior()

    {

        Vector3 nextPosition = guardPosition;

        if (patrolPath != null)

        {

            if (AtWaypoint())

            {

                timeSinceArrivedAtWaypoint = 0;

                SuspicionTrigger();

                CycleWaypoint();

            }

            nextPosition = GetCurretWaypoint();

        }

        if (timeSinceArrivedAtWaypoint > waypointDwellTime)

        {

            mover.StartMoveAction(nextPosition);

        }

    }

    private Vector3 GetCurretWaypoint()

    {

        return patrolPath.GetWayPoint(currentWaypointIndex);

    }

    private void CycleWaypoint()

    {

        currentWaypointIndex = patrolPath.GetNextIndex(currentWaypointIndex);

    }

    private bool AtWaypoint()

    {

        float distanceToWaypoint = Vector3.Distance(transform.position, GetCurretWaypoint());

        return distanceToWaypoint < waypointTolerance;

    }

    private void SuspicionBehavior()

    {

        GetComponent<ActionScheduler>().CancelCurrrentAction();

    }

thanks again

It looks like in every frame you’re resetting and setting the Suspicion animation trigger (when in Suspicion). You’ll need to have a way to reset this so it’s only called once.

//Add this global variable.
bool suspicionTriggerSet=false;

/// then in Suspicion Trigger:
if(suspicionTriggerSet) return;
suspicionTriggerSet=true;
GetComponent<Animator>().ResetTrigger("suspicion");
GetComponent<Animator>().SetTrigger("suspicion");

And finally, add this to AttackBehavior
suspicionTriggerSet=false;
1 Like

this is how I originally was triggering the animation which was causing the original problem it would enter the transition regardless if it was true or false then realizes that its false and goes along its way and it happens every second until it arrives at the waypoint. I think its because the logic is happing in update to begin with. changing it the way I set it up fixes that problem now I just get a weird jitter when they return to the waypoint after chasing the player.

The Jitterbug™ is caused because the NavMeshAgent can never truly get you to your “exact” destination… the easiest way to stop this behavior is to check the NavMeshAgent’s remainingDistance for a “close enough” distance, and then use NavMeshAgent.isStopped=true; if it is.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms