Big Glasses
Big Glasses

Reputation: 43

Enemy teleporting between across the y axis, when it's under me while I'm on a platform, it teleports up the platform

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.

Upvotes: 1

Views: 48

Answers (1)

Big Glasses
Big Glasses

Reputation: 43

The problem was that I was constantly setting it to

x, 1, z 

in Update

Upvotes: 1

Related Questions