Help-me Script AI NavMesh Follow Player

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class DamageHandler_Enemy : vp_DamageHandler
{
private Animator _animator;
private NavMeshAgent _navMeshAgent;
public GameObject Player;
public float AttackDistance = 10.0f;

protected override void Awake()

{
base.Awake();
_navMeshAgent = GetComponent();
_animator = GetComponent();
}

public void Update()

{

if (_navMeshAgent.enabled)

{
float dist = Vector3.Distance(Player.transform.position, this.transform.position);
if (dist < AttackDistance)

{

_navMeshAgent.SetDestination(Player.transform.position);
_animator.SetBool(“Attack”, false);
_animator.SetBool(“IsFollow”, true);

}
else
{

_animator.SetBool(“Attack”, false);
_animator.SetBool(“IsFollow”, false);
_navMeshAgent.SetDestination(transform.position);

}

if (dist < 3.0f)

{
_animator.SetBool(“Attack”, true);
_animator.SetBool(“IsFollow”, false);
}
}
}

public override void Damage(vp_DamageInfo damageInfo)

{

if (CurrentHealth > 0 )

{

AnimatorStateInfo si = _animator.GetCurrentAnimatorStateInfo(0);
if (!si.IsName(“Attack”))

{

base.Damage(damageInfo);
_animator.Play(“Hit”, 0, 0.25f);

}

}

}
public override void Die()

{
if (!enabled || !vp_Utility.IsActive(gameObject))

return;

if (m_Audio != null)

{

m_Audio.pitch = Time.timeScale;
m_Audio.PlayOneShot(DeathSound);

}

_navMeshAgent.enabled = false;
_animator.SetBool(“IsFollow”, false);
_animator.SetBool(“Attack”, false);
_animator.SetTrigger(“Dying”);
Destroy(GetComponent<vp_SurfaceIdentifier>());
}
public void EndAttack()

{
float dist = Vector3.Distance(Player.transform.position, this.transform.position);

if(dist < 3.0f)

{
Player.SendMessage(“Damage”, 4.0f, SendMessageOptions.DontRequireReceiver);
}

}

}

ERROR:

“SetDestination” can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
DamageHandler_Enemy:Update() (at Assets/DamageHandler_Enemy.cs:69)
LINE 69:
_navMeshAgent.SetDestination(Player.transform.position);

It seems your eyes are bigger than stomach. There are Masses of informative lessons that can speed up your development time, try them out. A community example would be HERE.

From the compiler error it seems you need to bake a navmesh on your scene. In the top tool bar navigate to Window/Navigation/Bake.

1 Like

Thanks