Getting an error " “Stop” can only be called on an active agent that has been placed on a NavMesh.UnityEngine.StackTraceUtility:ExtractStackTrace () ".
Error on Mover.cs line 41
Error on Fighter.cs line 43
I did double check with enemy and player perfab variants have NavMeshAgent ( also in the secen enemy and player have NavMeshAgent).
Mover.cs
using UnityEngine;
using UnityEngine.AI;
using NR_RPG.Combat;
using NR_RPG.Core.ActionScheduler;
namespace NR_RPG.Movement
{
public class Mover : MonoBehaviour, IAction
{
NavMeshAgent navMeshAgent;
Health health;
private void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
health = GetComponent<Health>();
}
private void Update()
{
navMeshAgent.enabled = !health.IsDead();
UpdateAnimator();
}
public void StartMoveAction(Vector3 destination)
{
GetComponent<ActionScheduler>().StartAction(this);
GetComponent<Fighter>().Cancel();
MoveTo(destination);
}
public void MoveTo(Vector3 destination)
{
navMeshAgent.destination = destination;
navMeshAgent.isStopped = false;
}
public void Cancel()
{
navMeshAgent.isStopped = true;
}
private void UpdateAnimator()
{
Vector3 velocity = navMeshAgent.velocity;
Vector3 localVelcoity = transform.InverseTransformDirection(velocity);
float speed = localVelcoity.z;
GetComponent<Animator>().SetFloat("forwardSpeed", speed);
}
}
}
Fighter.cs
using UnityEngine;
using NR_RPG.Movement;
using NR_RPG.Core.ActionScheduler;
using NR_RPG.Core;
namespace NR_RPG.Combat
{
public class Fighter : MonoBehaviour, IAction
{
//SerializeFields
[SerializeField] float weaponRange = 1.75f;
[SerializeField] float timeBetweenAttack = 1.15f;
[SerializeField] float weaponDamge = 5f;
// Start Vars
Mover mover;
Animator animator;
// Private Vars
private Health _target;
private float _timeSinceLastAttack = Mathf.Infinity;
private void Start ()
{
mover = GetComponent<Mover>();
animator = GetComponent<Animator>();
}
private void Update()
{
_timeSinceLastAttack += Time.deltaTime;
if (_target == null) return;
if (_target.IsDead()) return;
if (!GetIsInRange())
{
mover.MoveTo(_target.transform.position);
}
else
{
mover.Cancel();
AttackBehavior();
}
}
private void AttackBehavior()
{
transform.LookAt(_target.transform);
if (_timeSinceLastAttack > timeBetweenAttack)
{
// This will trigger the Hit() event
TriggerAttack();
_timeSinceLastAttack = 0;
}
}
private void TriggerAttack()
{
animator.ResetTrigger("stopAttack");
animator.SetTrigger("attack");
}
//Animation Event Called by Unity
void Hit()
{
if (_target == null) return;
_target.TakeDamge(weaponDamge);
}
private bool GetIsInRange()
{
return Vector3.Distance(transform.position, _target.transform.position) < weaponRange;
}
public bool CanAttack(GameObject combatTarget)
{
if(combatTarget == null) return false;
Health targetToTest = combatTarget.GetComponent<Health>();
return targetToTest != null && !targetToTest.IsDead();
}
public void Attack(GameObject combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
_target = combatTarget.GetComponent<Health>();
}
public void Cancel()
{
StopAttack();
_target = null;
}
private void StopAttack()
{
animator.ResetTrigger("attack");
animator.SetTrigger("stopAttack");
}
}
}```
the game is not broken everything is working, any ideas how to get ride of the error?
Thank you in advance for the help.
Nathon