-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemySpawnerScript.cs
More file actions
82 lines (69 loc) · 2.47 KB
/
Copy pathEnemySpawnerScript.cs
File metadata and controls
82 lines (69 loc) · 2.47 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawnerScript : MonoBehaviour
{
[SerializeField]private GameObject[] enemies;
[SerializeField] private Collider spawnRange1Collider;
[SerializeField] private Collider spawnRange2Collider;
[SerializeField] private Collider spawnRange3Collider;
private int spawnRange;
// Start is called before the first frame update
void Start()
{
spawnRange = PlayerPrefs.GetInt("SpawnRange", 3);
}
// Update is called once per frame
void Update()
{
}
private bool spaceTakenCondition;
private bool spawnRangeCondition;
private Vector3 positionToSpawn;
public void SpawnEnemyAtRandomPosition(int times)
{
for (int i = 0; i < times; i++)
{
spawnRangeCondition = false;
spaceTakenCondition = false;
while (!spaceTakenCondition || !spawnRangeCondition)
{
FindAptPos();
}
SpawnAtPos(positionToSpawn);
}
}
void FindAptPos()
{
float x = Random.Range(-20, 20);
float y = Random.Range(1, 2);
float z = Random.Range(-20, 20);
positionToSpawn = new Vector3(x, y, z);
spaceTakenCondition = Physics.OverlapSphere(positionToSpawn, 0.7f).Length > 0;
switch (spawnRange)
{
case 1:
spawnRangeCondition = ColliderContainsPoint(spawnRange1Collider.transform, positionToSpawn);
break;
case 2:
spawnRangeCondition = ColliderContainsPoint(spawnRange2Collider.transform, positionToSpawn);
break;
case 3:
spawnRangeCondition = ColliderContainsPoint(spawnRange3Collider.transform, positionToSpawn);
break;
}
}
void SpawnAtPos(Vector3 pos)
{
GameObject randomEnemy = enemies[Random.Range(0, enemies.Length)];
Instantiate(randomEnemy, pos, Quaternion.identity);
}
bool ColliderContainsPoint(Transform ColliderTransform, Vector3 Point, bool Enabled = true)
{
Vector3 localPos = ColliderTransform.InverseTransformPoint(Point);
if (Enabled && Mathf.Abs(localPos.x) < 0.5f && Mathf.Abs(localPos.y) < 0.5f && Mathf.Abs(localPos.z) < 0.5f)
return true;
else
return false;
}
}