Hi, I have an enemy that changes betwen two states (One that follows an GameObject Objetive, and other that follows a seat of Waypoints at random)
But its not working correctly, when it follows an Objetive it goes after it touches and when its state changes to SeguirWaypoints it just goes to the nearest one at doesnt move or goes another waypoint.
using UnityEngine;
using UnityEngine.AI;
public class CaballoPathLogic : MonoBehaviour
{
protected enum EstadosCaballo
{
SeguirWaypoints,
SeguirObjetivo
}
[SerializeField] EstadosCaballo estados = EstadosCaballo.SeguirWaypoints;
[Header("Objetivo")]
public GameObject Objetivo;
[Header("NavMesh")]
private NavMeshAgent NavMesh;
[Header("Colliders")]
public SphereCollider ColliderAtaque;
public SphereCollider ColliderVision;
public SphereCollider ColliderAbuela;
[Header("Rango Colliders")]
public float RangoAtaque = 1;
public float RangoVision = 1;
[Header("Waypoints")]
CaballoWaypoints WaypointActual = null;
CaballoWaypoints[] Waypoints;
[Header("Tiempos")]
public float TiempoAtacando;
public float MaximoTiempoAtacando;
public float TiempoQuieto;
public float MaximoTiempoQuieto;
private Vector3 UltimaPosicion;
[Header("Velocidades")]
public float VelocidadMinima;
public float VelocidadPatrullar;
public float VelocidadIrHaciaObjetivo;
public float VelocidadActual;
private void Awake()
{
ColliderAtaque = this.GetComponent<SphereCollider>();
NavMesh = this.GetComponent<NavMeshAgent>();
GameObject[] waypoint = GameObject.FindGameObjectsWithTag("CaballoWaypoints");
Waypoints = new CaballoWaypoints[waypoint.Length];
for (int i = 0; i < waypoint.Length; i++)
{
Waypoints[i] = waypoint[i].GetComponent<CaballoWaypoints>();
}
}
void Start()
{
ColliderAtaque.radius = RangoAtaque;
ColliderVision.radius = RangoVision;
}
void Update()
{
if (BotonPausa.current.Pausa)
return;
Comportamiento();
}
private void Comportamiento()
{
VelocidadActual = (transform.position - UltimaPosicion).magnitude / Time.deltaTime;
UltimaPosicion = transform.position;
if (TiempoAtacando < MaximoTiempoAtacando)
{
TiempoAtacando += Time.deltaTime;
return;
}
if (Objetivo != null)
{
PerseguirObjetivo();
}
if (Objetivo == null)
{
Patrullar();
}
ComprobarTiempoQuieto();
}
private void PerseguirObjetivo()
{
estados = EstadosCaballo.SeguirObjetivo;
NavMesh.speed = VelocidadIrHaciaObjetivo;
NavMesh.SetDestination(Objetivo.transform.position);
}
private void Patrullar()
{
estados = EstadosCaballo.SeguirWaypoints;
NavMesh.speed = VelocidadPatrullar;
CaballoWaypoints waypoint = BuscarWaypointMasCercano();
NavMesh.SetDestination(waypoint.transform.position);
}
private void ComprobarTiempoQuieto()
{
if (VelocidadActual <= VelocidadMinima)
{
TiempoQuieto += Time.deltaTime;
}
else
{
TiempoQuieto = 0;
}
if (TiempoQuieto >= MaximoTiempoQuieto)
{
DefinirObjetivo(null);
}
}
public void AlColisionar(Collider colision, GameObject objetivo)
{
if (colision == ColliderVision)
{
Debug.Log("Voy hacia el Objetivo");
DefinirObjetivo(objetivo);
return;
}
if (colision == ColliderAtaque && Objetivo != null)
{
Debug.Log("Ataco Objetivo");
AtacarObjetivo();
}
}
private void AtacarObjetivo()
{
TiempoAtacando = 0;
DefinirObjetivo(null);
}
private void DefinirObjetivo(GameObject objetivo)
{
Objetivo = objetivo;
}
public void DefinirWaypoint(CaballoWaypoints waypointActual)
{
if (estados != EstadosCaballo.SeguirObjetivo)
{
WaypointActual = waypointActual;
NavMesh.SetDestination(WaypointActual.transform.position);
}
}
CaballoWaypoints BuscarWaypointMasCercano()
{
if (Waypoints.Length > 0)
{
float[] distancia = new float[Waypoints.Length];
float ValorMásPequeño = Vector3.Distance(transform.position, Waypoints[0].transform.position);
int IndexMásPequeño = 0;
for (int i = 0; i < Waypoints.Length; i++)
{
float temporal = Vector3.Distance(transform.position, Waypoints[i].transform.position);
if (temporal < ValorMásPequeño)
{
ValorMásPequeño = temporal;
IndexMásPequeño = i;
}
}
return Waypoints[IndexMásPequeño];
}
return null;
}
}
using UnityEngine;
[RequireComponent(typeof(Collider))]
public class CaballoWaypoints : MonoBehaviour
{
[Header("Waypoints")]
public CaballoWaypoints[] Waypoints;
[Header("Colliders")]
private Collider ColliderWaypoint;
private void Awake()
{
ColliderWaypoint = this.GetComponent<Collider>();
}
private void OnTriggerEnter(Collider este)
{
if (este.tag.Equals("Caballo"))
{
if (Waypoints.Length > 0)
{
System.Random aleatorio = new System.Random();
int IndexAleatorio = aleatorio.Next(0, Waypoints.Length);
CaballoPathLogic logica = este.gameObject.GetComponent<CaballoPathLogic>();
if (logica)
{
Debug.Log("Paso por :" + Waypoints[IndexAleatorio].name);
logica.DefinirWaypoint(Waypoints[IndexAleatorio]);
}
}
}
}