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?