i made a script for my enemy and i used NavMeshAgent component. Everything works but when i go on certain locations it just stops walking. i made a screenshot. When i am on the green places it still comes to me but when i am on the red locations it stops walking
tempsnip hosted at ImgBB — ImgBB ← screenshot
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class KI_Walking : MonoBehaviour
{
public NavMeshAgent agent;
public int speed;
public Transform player;
public LayerMask whatIsGround, whatIsPlayer;
//States
public float sightRange, attackRange;
public bool visualEnemy;
public bool playerInSightRange, playerInAttackRange;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
agent.speed = speed;
//Check for sight and attack range
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if (playerInSightRange && !playerInAttackRange) ChasePlayer();
if (playerInAttackRange && playerInSightRange && visualEnemy) AttackPlayer();
}
private void ChasePlayer()
{
agent.SetDestination(player.position);
}
private void AttackPlayer()
{
//Make sure enemy doesn't move
agent.SetDestination(transform.position);
transform.LookAt(player);
PlayerManager Player;
Player = FindObjectOfType<PlayerManager>();
Player.health--;
}
private void DestroyEnemy()
{
Destroy(gameObject);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, attackRange);
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, sightRange);
}
}