-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathObstacleCreate
137 lines (108 loc) · 4.42 KB
/
ObstacleCreate
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleCreate : MonoBehaviour
{
List<GameObject> Prefabs = new List<GameObject>();
Transform tr;
public GameObject prefabAntfriend;
public GameObject prefabWorm;
public GameObject prefabBark;
public GameObject prefabHole;
//public float mintime = 5.0f; //중간에 랜덤으로 나오게 할때 최소 시간 간격
//public float maxtime = 7.0f; //중간에 랜덤으로 나오게 할때 최대 시간 간격
public float ltx = -10.9f; //x좌표 왼쪽 위치
public float rtx = -13.1f; //x좌표 오른쪽 위치
public int miny = 4; //y좌표 최소 위치
public int maxy = 35; //y좌표 최대 위치
public int interval = 3;
public List<float> y_interval = new List<float>();
public float rz = 6.1f; //z좌표값 고정
List<float> rx = new List<float>(); //x좌표값들
List<int> ry = new List<int>(); //y좌표값들
public int num = 10; //장애물 개수
public bool chkObs = true;
int prefab_leng, y_interval_leng;
Vector3 limitMin, limitMax; //Gizmos 표현하기 위한 좌표값
void Start()
{
tr = GameObject.Find("ant").GetComponent<Transform>();
//장애물의 종류가 랜덤으로 선택되도록 Prefabs 리스트에 장애물프리팹 추가
GameObject _obj = prefabAntfriend as GameObject;
Prefabs.Add(_obj);
_obj = prefabWorm as GameObject;
Prefabs.Add(_obj);
_obj = prefabBark as GameObject;
Prefabs.Add(_obj);
_obj = prefabHole as GameObject;
Prefabs.Add(_obj);
prefab_leng = Prefabs.Count;
y_interval_leng = y_interval.Count;
//x좌표 추가
rx.Add(ltx);
rx.Add(rtx);
//StartCoroutine(CreateBark()); //중간에 랜덤으로 생성되게 하는 코드
}
void Update()
{
if (tr.position.y <= 0.0f && chkObs)
{
chkObs = false;
ry = new List<int>(); //초기화
//y좌표 추가 위아래 간격이 있어야 개미가 이동가능하므로 interval으로 간격줌
for (int i = miny; i <= maxy; i += interval)
{
ry.Add(i);
}
//처음 시작 시 랜덤으로 num개수만큼 장애물 나옴
for (int i = 0; i < num; i++)
{
int x_id = Random.Range(0, 2);
int y_id = Random.Range(0, ry.Count);
int y_interval_id = Random.Range(0, y_interval_leng);
Vector3 creatingPoint = new Vector3(rx[x_id], (float)ry[y_id] + y_interval[y_interval_id], rz);
int prefab_id = Random.Range(0, prefab_leng);
//Antfiend가 가로로 눕히는 과정에서 나무 밖으로 빗겨나가므로 수동으로 x좌표값 추가
if (prefab_id == 0)
{
creatingPoint = new Vector3(rx[x_id] + 0.55f, ry[y_id], rz);
}
Instantiate(Prefabs[prefab_id], creatingPoint, Quaternion.identity);
//y좌표값 중복 방지
ry.Remove(ry[y_id]);
}
}
}
/* //게임 중간에 랜덤으로 나왔다 사라지고 나왔다 사라지는 코드
IEnumerator CreateBark()
{
while (true)
{
for (int i = 0; i < 4; i++)
{
int x_id = Random.Range(0, 2);
int y_id = Random.Range(0, ry.Count);
Vector3 creatingPoint = new Vector3(rx[x_id], ry[y_id], rz);
int prefab_id = Random.Range(0, 4);
if (prefab_id == 0)
{
creatingPoint = new Vector3(rx[x_id] + 0.5f, ry[y_id], rz);
}
Instantiate(Prefabs[prefab_id], creatingPoint, Quaternion.identity);
ry.Remove(ry[y_id]);
}
float creatingTime = Random.Range(mintime, maxtime);
yield return new WaitForSeconds(creatingTime);
}
}*/
private void OnDrawGizmos()
{
Gizmos.color = Color.blue;
limitMin = new Vector3(ltx, miny, rz);
limitMax = new Vector3(ltx, maxy, rz);
Gizmos.DrawLine(limitMin, limitMax);
limitMin = new Vector3(rtx, miny, rz);
limitMax = new Vector3(rtx, maxy, rz);
Gizmos.DrawLine(limitMin, limitMax);
}
}