HI, when i kill an enemy the game crashes and i get this error:
“Stop” can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.StackTraceUtility:ExtractStackTrace ()
here is my code from Mover:
using Rpg.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
namespace Rpg.Movement
{
public class Mover : MonoBehaviour,IAction
{
[SerializeField] private Transform target;
[SerializeField] private float maxSpeed = 6f;
NavMeshAgent navMeshAgent;
Health health;
// Start is called before the first frame update
void Start()
{
health= GetComponent<Health>();
navMeshAgent= GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
navMeshAgent.enabled = !health.isDead;
UpdateAnimator();
}
private void UpdateAnimator()
{
Vector3 velocity = GetComponent<NavMeshAgent>().velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
GetComponent<Animator>().SetFloat("fowardSpeed", speed);
}
public void Cancel()
{
navMeshAgent.isStopped= true;
}
public void MoveTo(Vector3 destination, float speedFraction)
{
navMeshAgent.isStopped = false;
navMeshAgent.speed = maxSpeed * Mathf.Clamp01(speedFraction);
navMeshAgent.destination = destination;
}
public void StartMoveAction(Vector3 destination,float speedFraction)
{
GetComponent<ActionScheduler>().StartAction(this);
MoveTo(destination,speedFraction);
}
}
}
Here is my fighter code :
using Rpg.Core;
using Rpg.Movement;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Rpg.Combat
{
public class Fighter : MonoBehaviour, IAction
{
[SerializeField] Weapon defaultWeapon = null;
[SerializeField] Transform handTransform = null;
private Health target;
float timeSinceLastAttack = Mathf.Infinity;
Weapon currentWeapon= null;
private void Start()
{
EquipWeapon(defaultWeapon);
}
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
if (target == null) return;
if (target.isDead) return;
if (!GetIsInRange())
{
GetComponent<Mover>().MoveTo(target.transform.position, 1f);
}
else
{
GetComponent<Mover>().Cancel();
AttackBehavior();
}
}
public void EquipWeapon(Weapon weapon)
{
currentWeapon= weapon;
Animator animator = GetComponent<Animator>();
weapon.Spawn(handTransform,animator);
}
private void AttackBehavior()
{
transform.LookAt(target.transform);
if (timeSinceLastAttack > currentWeapon.GetTimeBetweenAttack())
{
//trigger hit event
TriggerAttack();
timeSinceLastAttack = 0f;
}
}
private void TriggerAttack()
{
GetComponent<Animator>().ResetTrigger("stopAttack");
GetComponent<Animator>().SetTrigger("attack");
}
//Animation event
void Hit()
{
if (target == null) return;
target.TakeDamage(currentWeapon.GetDamage());
}
private bool GetIsInRange()
{
return Vector3.Distance(transform.position, target.transform.position) < currentWeapon.GetRange();
}
public void Attack(GameObject combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
target = combatTarget.GetComponent<Health>();
}
public void Cancel()
{
GetComponent<Mover>().Cancel();
StopAttack();
target = null;
}
private void StopAttack()
{
GetComponent<Animator>().ResetTrigger("attack");
GetComponent<Animator>().SetTrigger("stopAttack");
}
public bool CanAttack(GameObject combatTarget)
{
if (combatTarget == null) return false;
Health targetToTest = combatTarget.GetComponent<Health>();
return targetToTest != null && !targetToTest.isDead;
}
}
}
Thanks !