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.