-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpawnWithAIFollow.cs
More file actions
50 lines (41 loc) · 1.83 KB
/
SpawnWithAIFollow.cs
File metadata and controls
50 lines (41 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class SpawnWithAIFollow : MonoBehaviour
{
public List<GameObject> prefabsToSpawn; // List of prefabs to spawn with AI
// Public variables to set AIFollow and NavMeshAgent properties
public float wanderRadius = 10f;
public float wanderTimer = 5f;
public float followRange = 15f;
public float stopDistance = 10f;
public float respawnTimer = 10f; // Time between respawns
void Start()
{
StartCoroutine(SpawnObjects());
}
IEnumerator SpawnObjects()
{
while (true)
{
// Choose a random prefab from the list
int randomIndex = Random.Range(0, prefabsToSpawn.Count);
GameObject prefabToSpawn = prefabsToSpawn[randomIndex];
// Spawn the chosen prefab at the current gameObject's position and rotation
GameObject spawnedObject = Instantiate(prefabToSpawn, transform.position, transform.rotation);
// Attach NavMeshAgent to the spawned object
NavMeshAgent navMeshAgent = spawnedObject.AddComponent<NavMeshAgent>();
navMeshAgent.speed = 5f; // Set NavMeshAgent speed (adjust as needed)
// Attach AIFollow script to the spawned object
AIFollow aiFollowScript = spawnedObject.AddComponent<AIFollow>();
// Assign values from spawner to AIFollow script
aiFollowScript.wanderRadius = wanderRadius;
aiFollowScript.wanderTimer = wanderTimer;
aiFollowScript.followRange = followRange;
aiFollowScript.stopDistance = stopDistance;
// Wait for respawn timer before spawning the next object
yield return new WaitForSeconds(respawnTimer);
}
}
}