1

I don't know if it's something with the AI, the navmesh, or maybe a missing nav component but the enemy I made instantly teleports to me when I'm above it instead of simply going up the stairs. It is normal when we have the same value in position for the y axis but it starts teleporting up when the y axis changes.

I've tried redoing the navmesh, I've tried changing around the prefab values and I just cant figure out what to do.

I'll leave the code and the values of the enemy object inspector below.

{
private NavMeshAgent agent;
public LayerMask playerLayer;
private WaveSpawner waveSpawner;
public GameObject[] allEnemies;
public GameObject closestEnemy;

//variables
public float sightRange = 10f;
public float attackRange = 1f;
public float patrolRange = 3f;
bool inSightRange;

private void Start()
{
    waveSpawner = GetComponent<WaveSpawner>();
    agent = GetComponent<NavMeshAgent>();
}

private void Update()
{
    allEnemies = GameObject.FindGameObjectsWithTag("Player");
    closestEnemy = ClosestEnemy();

    transform.position = new Vector3(transform.position.x, 1, transform.position.z);

    NavMeshAgent agent = GetComponent("NavMeshAgent") as NavMeshAgent;
    NavMeshHit closestHit;
    if (NavMesh.SamplePosition(transform.position, out closestHit, 100f, NavMesh.AllAreas))
    {
        transform.position = closestHit.position;
        agent.enabled = true;
    }

    if (Physics.CheckSphere(transform.position, sightRange, playerLayer))
    {
        Chase();
    }
    else
        inSightRange = false;

    if (!inSightRange && Vector3.Distance(transform.position, agent.destination) < patrolRange)
    {
        Patrolling();
    }
}

GameObject ClosestEnemy()
{
    GameObject closestHere = gameObject;
    float leastDistance = Mathf.Infinity;

    foreach (var enemy in allEnemies)
    {
        float distanceHere = Vector3.Distance(transform.position, enemy.transform.position);
        if (distanceHere < leastDistance)
        {
            leastDistance = distanceHere;
            closestHere = enemy;
        }
    }

    return closestHere;
}
private void OnDrawGizmos()
{
    Gizmos.DrawSphere(transform.position, sightRange);
}

void Patrolling()
{
    float searchZ = Random.Range(-sightRange, sightRange);
    float searchX = Random.Range(-sightRange, sightRange);
    agent.destination = new Vector3(transform.position.x + searchX, 1, transform.position.z + searchZ);
}

void Chase()
{

        agent.destination = (closestEnemy.transform.position);
        inSightRange = true;
}

}

Enemy components and values

Part 2 of enemy components and values

Edit, after additional testing, I've discovered that it's the 3d physical object of the enemy that goes through the floor and not the enemy itself.

2
  • so every frame you're going to move the player to x,1,z and then test if its on the navmesh and move it t the nearest navmesh position? that seems a lot - add some debugging to your code watch what goes on - also i thought SetDestination was the correct way to send your object using the navmesh, not directly setting the property
    – BugFinder
    Commented Jul 8 at 9:13
  • SetDestination doesn't seem to exist in unity. but I will try removing the constant x, 1, z Commented Jul 8 at 18:36

1 Answer 1

1

The problem was that I was constantly setting it to

x, 1, z 

in Update

Not the answer you're looking for? Browse other questions tagged or ask your own question.