Help With AI Mesh

Wave Spawner Code

using UnityEngine;
using System.Collections;
using Unity.AI.Navigation;
using System.Collections.Generic;
using UnityEngine.AI;
public class WaveSpawner : MonoBehaviour
{
    [SerializeField] private float countdown;
    [SerializeField] private GameObject spawnPoint;

    public Wave[] waves;
    public int currentWaveIndex = 0;
    public NavMeshSurface navSurface;



    private bool readyToCountDown;
    private void Start()
    {
        readyToCountDown = true;

        for (int i = 0; i < waves.Length; i++)
        {
            waves[i].enemiesLeft = waves[i].enemies.Length;
        }
    }
    private void Update()
    {

        if (currentWaveIndex >= waves.Length)
        {
            Debug.Log("You survived every wave!");
            return;
        }

        if (readyToCountDown == true)
        {
            countdown -= Time.deltaTime;
        }

        if (countdown <= 0)
        {
            readyToCountDown = false;

            countdown = waves[currentWaveIndex].timeToNextWave;

            StartCoroutine(SpawnWave());
        }

        if (waves[currentWaveIndex].enemiesLeft == 0)
        {
            readyToCountDown = true;

            currentWaveIndex++;
        }
    }
    private IEnumerator SpawnWave()
    {
        navSurface.BuildNavMesh();
        if (currentWaveIndex < waves.Length)
        {
            for (int i = 0; i < waves[currentWaveIndex].enemies.Length; i++)

            {
                Enemy enemy = Instantiate(waves[currentWaveIndex].enemies[i], spawnPoint.transform);

             
                enemy.transform.SetParent(spawnPoint.transform);

                yield return new WaitForSeconds(waves[currentWaveIndex].timeToNextEnemy);

[B]Enemy Follow Code:[/B]
[code=CSharp]using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Build;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;

public class EnemyFollow : MonoBehaviour
{
    public NavMeshAgent enemy;
    public GameObject Player;
    public float lookRadius = 10f;
    private Renderer Renderer;
    public GameObject Bullet;
    public Transform BulletFire;
    private float LastTime;
    RaycastHit hitEnemy;
    // Start is called before the first frame update
    void Start()
    {
        Renderer = GetComponent<Renderer>();
    }

    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(Player.transform.position,transform.position);
        if (distance <= lookRadius) {
            enemy.SetDestination(Player.transform.position);
        }
        if (distance <= enemy.stoppingDistance)
        {
            #region Attacking Code
            Renderer.material.color = Color.red;
            ShootEnemy();
            #endregion
            FaceTarget();
        }
    }

    void FaceTarget()
    {
        Vector3 direction = (Player.transform.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    }

    public void ShootEnemy()
    {
            if(Time.time > LastTime + 0.50f) {
            Rigidbody rb = Instantiate(Bullet, BulletFire.position, Quaternion.identity).GetComponent<Rigidbody>();
            rb.AddForce(transform.forward * 22f, ForceMode.Impulse);
            rb.AddForce(transform.up * 2f , ForceMode.Impulse);
            LastTime = Time.time;
        }
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, lookRadius);
    }
}

}
}
}
}

[System.Serializable]
public class Wave
{
public Enemy[ ] enemies;
public float timeToNextEnemy;
public float timeToNextWave;

[HideInInspector] public int enemiesLeft;
}[/code]

Video:
9wvtbr

I have two script files each attached to two enemy prefabs. Does anyone know a fix for why is it not activating my Agent as it says its out of range of the mesh although the spread does cover it. Why also is my enemies spawning far away from my spawn point. I am new to unity so any help would be appreciated.

It doesn’t look like you’re actually setting the position of the enemy after you instantiate it.

You could do:

enemy.transform.SetParent(spawnPoint.transform);
enemy.transform.localPosition = Vector3.zero;

public void SetParent(Transform parent, bool worldPositionStays);
worldPositionStays appears to be bugged in my version of Unity (2017.4) and is always ignored for some reason.

@A

Thank you for the prompt reply, after trying this I get an error:
9815310--1410237--upload_2024-5-5_20-32-15.png
do you know why this error may be occurring? Thank you

You don’t need to add that line of code anywhere. It’s part of Transform in the API. All you need to do is set the local position of enemy after you cal SetParent

Thank you very much this has fixed one of my major problems in my project. I dont suppose you have experience with NavMashAgents. As although the enemy spawns in the right position now it still wont activate my NavMeshSurface. If you need to see any additional information let me know but the code above mentions all instances where the NavMesh/Agent is mentioned. Thank you, I appreciate it
8f42zj

Are you able to generate an see the blue nav mesh when not in playmode?

Yes, please see image:
https://imgur.com/a/7Gnrdpx

I’m not knowledgeable with baking navmeshes at runtime.
Do you need to bake the navmesh at runtime? Can’t you just use the navmesh that was baked in the Editor mode?
As I understand it, the need to bake the navmesh at runtime is normally done only if the terrain is changing in playmode.

Just deleted the line of code, still doesnt work however wondering if thats because of the size of my new Navmesh, Please see image https://imgur.com/a/tBZ2dDY
Really appreciate your help, this is a major assignment and I need to get this fixed to submit it.

have you tried moving the enemy to the nearest navmesh position? this might give you some clues, you may also find depending on where the pivot point of the enemy is it may feel it never gets to the navmesh…

There are a lot of tutorials on navmesh, and your blurry video was too hard to read what was going on

You could look at the scene view as this is happening so you can see the gizmo for the navagent. This will tell you where the agent is.
I don’t know your setup, but In my setups, I have the agent and capsule be seperate gameObjects. The capsule would then follow the agent.

In your code you have:

if (distanceToPlayer <= lookRadius) {
    enemy.SetDestination(Player.transform.position);
 }

Is this correct? Or should it be?:
if(distanceToPlayer > lookRadius)

From your video, it looks like the enemy is spawning on top of the player. Maybe it should spawn next to the enemy.

Thank you, I managed to fix the issue as I found the object wasent actually linked to the player and therefore would have this issue. I made a new post for the NavMesh as this is the last issue I am encounting.
https://discussions.unity.com/t/946681