I am using NavMesh Pro for making my enemy follow my player in a 2d game, but my enemy cannot follow my player if its directly above it, in the same x position. At that point my enemy freezes unless I move in the x axis. How can I solve this?
This is my code for the enemy:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class scrRobot : scrEnemigoBase
{
public GameObject ataque1;
public Transform attackPoint;
[HideInInspector] public NavMeshAgent agent;
public GameObject bola;
public override void Start()
{
base.Start();
cooldown = 100;
agent = GetComponent<NavMeshAgent>();
agent.updateRotation = false;
agent.updateUpAxis = false;
agent.speed = spd;
}
public override void Update()
{
base.Update();
if (agent != null)
{
agent.SetDestination(player.transform.position);
}
if (cooldown > 0)
{
cooldown -= Time.deltaTime;
} else
{
if (canAttack)
{
StartCoroutine(Ataque());
}
}
}
private IEnumerator Ataque()
{
agent.speed = 0;
canAttack = false;
animator.SetFloat("anim", 1);
yield return new WaitForSeconds(1.5f);
Instantiate(bola, attackPoint.position, Quaternion.identity);
animator.SetFloat("anim", 2);
canAttack = true;
cooldown = Random.Range(5, 8);
agent.speed = spd;
}
}
