Hello everyone.
I have a problem with the movement of my enemies.
What I want is for them to follow the closest opponent (they already do) but being very close they stay on top of each other. The idea is that they turn like a car, when they cross each other, the idea is that they turn around and find each other.
My script:
public class MovimientoIA : MonoBehaviour
{
public float velocidad;
public float angulo;
private Rigidbody2D rb;
private Transform enemigo;
[Header("tag")]
public string etiqueta;
void Start()
{
rb = gameObject.transform.GetComponent<Rigidbody2D>();
BuscarObjetivo();
}
void Update()
{
// Rotation
Vector3 objetivo = enemigo.position - transform.position;
objetivo.z = 0;
Vector3 direccion = transform.up;
direccion.z = 0;
float angulo = Vector3.SignedAngle(direccion, objetivo, Vector3.forward);
angulo = Mathf.Clamp(angulo, -360f, 360f);
transform.Rotate(Vector3.forward, angulo);
// Movement
rb.velocity = transform.up * velocidad;
}
private void BuscarObjetivo(){
if(etiqueta == "rojo"){
enemigo = GameObject.FindGameObjectWithTag("azul").transform;
}
else if(etiqueta == "azul"){
enemigo = GameObject.FindGameObjectWithTag("rojo").transform;
}
}
}
What I can do?