good afternoon,
I’m having trouble getting my character to be chased by the enemy (navMeshAgent), I have an area of range for the enemy to be active, and it works, but the enemy is running still, I believe that’s the problem.
“player = GameObject.FindGameObjectWithTag(“Player”).transform;”
I tried in different ways to correct the code but nothing was possible, the BAKE of the mesh is correct and the animations too, I also keep getting this error on the screen that I couldn’t find out what it refers to:
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Graphs.Edge.WakeUp () (at <834c608fccf644818e20b78d1d66c782>:0)
UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List1[T] inEdges, System.Collections.Generic.List
1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at <834c608fccf644818e20b78d1d66c782>:0)
UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at <834c608fccf644818e20b78d1d66c782>:0)
UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at <834c608fccf644818e20b78d1d66c782>:0)
UnityEditor.Graphs.Graph.WakeUp () (at <834c608fccf644818e20b78d1d66c782>:0)
UnityEditor.Graphs.Graph.OnEnable () (at <834c608fccf644818e20b78d1d66c782>:0)
follow enemy code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Enemys : MonoBehaviour
{
[Header("Atributos")]
public float vida;
public float dano;
public float speed;
public float lookRadius;
public float colliderRadius = 2f;
[Header("Componentes")]
public Animator anim;
public CapsuleCollider capsule;
public NavMeshAgent agent;
[Header("Others")]
public Transform player;
public bool attacking;
public bool walking;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
capsule = GetComponent<CapsuleCollider>();
agent = GetComponent<NavMeshAgent>();
player = GameObject.FindGameObjectWithTag("Player").transform;
}
// Update is called once per frame
void Update()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
float distance = Vector3.Distance(player.position, transform.position);
if (distance <= lookRadius)
{
//agent.isStopped = false;
agent.SetDestination(player.position);
if (!attacking)
{
agent.SetDestination(player.position);
anim.SetBool("walk", true);
walking = true;
}
if (distance <= agent.stoppingDistance)
{
//agent.isStopped = true;
StartCoroutine("Attack");
}
else
{
//attacking = false;
}
}
else
{
//agent.isStopped = true;
anim.SetBool("walk", false);
walking = false;
attacking = false;
}
}
IEnumerator Attack()
{
attacking = true;
walking = false;
anim.SetBool("walk", false);
anim.SetBool("attack", true);
yield return new WaitForSeconds(1f);
GetPlayer();
//yield return new WaitForSeconds(1f);
}
void GetPlayer()
{
foreach (Collider c in Physics.OverlapSphere((transform.position + transform.forward * colliderRadius), colliderRadius))
{
if (c.gameObject.CompareTag("Player"))
{
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}