Stuttering of Animations and extremely slow move speed

So going through the Patrolling behavior lecture, after setting up the patrol , I am noticing that my character is Stuttering between walk/idle (Be advised I am currently using 2 Mixamo characters–Mutant and Paladin as my players in game, each has their own controller/Animations). I looked through a few other questions that seemed to register the same issue and found your response with the added lines of code-

   if (navMeshAgent.remainingDistance < 1f)
            {
                navMeshAgent.isStopped = true;
            }

HOWEVER, even after implementing this I am noticing that the stutter still happens. I follow the enemy model watching in scene view and it seems as soon as the gizmo tracking the chase radius touches the line on the patrol path the stuttering starts. Prior to implementing the patrol path or even if I just get the enemy away from the patrol path the walk/idle/attack animations work just fine. I also notice that in the Animator the forwardSpeed doesn’t go over .05, and does drop to -4.13

here is the AI Controller and the Patrol path scripting I have

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

namespace RPG.Control
{
    public class PatrolPath : MonoBehaviour
    {
        const float waypointGizmoRadius = .3f;

        private void OnDrawGizmos()
        {
            
            for (int i = 0; i < transform.childCount; i++)
            {
                int j = GetNextIndex(i);
                GetWayPoint(i);
                Gizmos.DrawLine(GetWayPoint(i), GetWayPoint(j));
            }
        }
        public int GetNextIndex(int i)
        {
            if(i +1 == transform.childCount)
            {
                return 0;
            }
            return i + 1;
        }
        public Vector3 GetWayPoint(int i)
        {
            return transform.GetChild(i).position;
        }
    }
}
using RPG.Combat;
using RPG.Core;
using RPG.Movement;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

namespace RPG.Control
{
    public class AIController : MonoBehaviour
    {
        [SerializeField] float chaseDistance = 5f;
      //  [SerializeField] NavMeshAgent navMeshAgent;
        [SerializeField] float suspicionTime = 3f;
        [SerializeField] PatrolPath patrolPath;
        [SerializeField] float wayPointTolerance = 2.0f;

        //float weaponRange;
        Fighter fighter;
        Health health;
        GameObject player;
        Mover mover;

        Vector3 guardPostion;
        Quaternion guardPositionRotation;
        float timeSinceLastSawPlayer = Mathf.Infinity;
        int currentWayPointIndex = 0;



        private void Start()
        {
           // navMeshAgent = GetComponent<NavMeshAgent>();
            fighter = GetComponent<Fighter>();
            health = GetComponent<Health>();
            player = GameObject.FindWithTag("Player");
            mover = GetComponent<Mover>();

            guardPostion = transform.position;
            
        }
        private void Update()
        {
            if (health.IsDead()) return;
            if(InAttackRangeOfPlayer() && fighter.CanAttack(player))
            {
                timeSinceLastSawPlayer = 0;
                AttackBehaviour();
            }
            //This section stops the AI from chasing--Possibly Delete so AI chases player until they zone out.
            else if (timeSinceLastSawPlayer < suspicionTime)
            {
                // Suspicion State-
                SuspicionBehavior();
            }
            else
            {
                PatrolBehavior();
            }
            timeSinceLastSawPlayer += Time.deltaTime;
            
        }

        private void PatrolBehavior()
        {
            Vector3 nextPosition = guardPostion;

            if(patrolPath != null) 
            {
                if(AtWayPoint())
                {
                    CycleWaypoint();
                }
                nextPosition = GetCurrentWaypoint();
            }
            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 SuspicionBehavior()
        {
            GetComponent<ActionScheduler>().CancelCurrentAction();
        }

        private void AttackBehaviour()
        {
            fighter.Attack(player);
        }

        //Checks to see if the Enemy is in range to attack the player
        private bool InAttackRangeOfPlayer()
        {
            float distanceToPlayer = Vector3.Distance(player.transform.position, transform.position);
            return distanceToPlayer < chaseDistance;
        }

        Called By Unity to Draw gizmos such as the Chase Radius
        private void OnDrawGizmosSelected()
        {
            Gizmos.color = Color.blue;
            Gizmos.DrawWireSphere(transform.position, chaseDistance);
        }


    }
}

Any thoughts on this?

1 error I know for sure which was a mistake when Uncommenting on the private void OnDrawGizmosSelected() the called by unity to draw gizmos such as the chase radius is actually commented out and the second was I forgot to add the mover script to the above question.

using RPG.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.AI;


namespace RPG.Movement
{
    public class Mover : MonoBehaviour, IAction
    {
        //[SerializeField] Transform target;

        NavMeshAgent navMeshAgent;
        Health health;
        //private bool isRunning;

        private void Start()
        {
            navMeshAgent = GetComponent<NavMeshAgent>();
            health = GetComponent<Health>();
        }

        void Update()
        {
            navMeshAgent.enabled = !health.IsDead();
            if (navMeshAgent.remainingDistance < 1f)
            {
                navMeshAgent.isStopped = true;
            }
            UpdateAnimator();
        }

        //Updates the animator for movement
        private void UpdateAnimator()
        {
            Vector3 velocity = navMeshAgent.velocity;
            Vector3 localVelocity = transform.InverseTransformDirection(velocity);
            float speed = localVelocity.z;

            GetComponent<Animator>().SetFloat("forwardSpeed", speed);
        }
        //The actual move
        public void StartMoveAction(Vector3 destination)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            MoveTo(destination);
        }
        //Used for the Click To Move game system to show where the player is going
        public void MoveTo(Vector3 destination)
        {

            navMeshAgent.destination = destination;
            navMeshAgent.isStopped = false;

        }
        //Cancels the current movement
        public void Cancel()
        {
            navMeshAgent.isStopped = true;
        }        
    }
}

Actually please disregard this I was able to figure out what was causing the stuttering of animations. In the Lecture under you advise

public int GetNextIndex(int i)
        {
            if(i + 1 == transform.childCount)
            {
                return 0;
            }
            return i + 1;
        }

I was just testing something and found out that with Unity 2021.3.27f1 it would need to look as follows to work. (found it on a pure fluke lol)

public int GetNextIndex(int i)
        {
            if(i + 1 >= transform.childCount)
            {
                return 0;
            }
            return i + 1;
        }

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

Privacy & Terms