質問するログイン新規登録

質問編集履歴

1

追記コード

2021/06/27 01:35

投稿

momonoki
momonoki

スコア21

title CHANGED
File without changes
body CHANGED
@@ -120,4 +120,127 @@
120
120
  }
121
121
  }
122
122
 
123
+ ```
124
+
125
+ 追記コード
126
+ ```
127
+ using System.Collections;
128
+ using System.Collections.Generic;
129
+ using UnityEngine;
130
+ using UnityEngine.UI;
131
+
132
+ public class crowdHuman : MonoBehaviour
133
+ {
134
+ private float chargeTime;//動く時間
135
+ private float timeCount;
136
+ private float speed;//移動スピード
137
+ private float speed_Min;//Minスピード
138
+ private float speed_Max;//Maxスピード
139
+ Vector3 direction;//移動方向
140
+ Vector3 pos;//自分の位置
141
+ Vector3 minMoveArea = new Vector3(-8.9f, -4.9f, 0);//移動範囲(最小)
142
+ Vector3 maxMoveArea = new Vector3(-4.9f, 4.9f, 0);//移動範囲(最大)
143
+ Vector3 WorldPoint;
144
+ public Sprite[] HumanSprites;
145
+ Sprite HumanSprite;
146
+
147
+ // Start is called before the first frame update
148
+ void Start()
149
+ {
150
+ chargeTime = 0.9f;
151
+
152
+ //開始時の移動方向を決める
153
+ direction = RandomDirection();
154
+
155
+ //移動スピードをランダムに決める
156
+ speed_Min = 0.01f;
157
+ speed_Max = 0.02f;
158
+ speed = Random.Range(speed_Min, speed_Max);
159
+
160
+ //自分の位置を取得
161
+ Transform myTransform = this.transform;
162
+ pos = myTransform.position;
163
+
164
+ HumanSprites = Resources.LoadAll<Sprite>("image");
165
+ int i = Random.Range(0, 3);
166
+ HumanSprite = HumanSprites[i];
167
+ this.GetComponent<Image>().sprite = HumanSprite;
168
+ }
169
+
170
+ // Update is called once per frame
171
+ void Update()
172
+ {
173
+ timeCount += Time.deltaTime;
174
+
175
+ WorldPoint = transform.TransformPoint(pos);
176
+
177
+ //枠から出たら逆方向を向く
178
+ //if ((pos.x < minMoveArea.x)||(pos.y < minMoveArea.y)||(pos.x > maxMoveArea.x)||(pos.y > maxMoveArea.y))
179
+ if ((WorldPoint.x < minMoveArea.x) || (WorldPoint.y < minMoveArea.y) || (WorldPoint.x > maxMoveArea.x) || (WorldPoint.y > maxMoveArea.y))
180
+ {
181
+ Turn();
182
+ //それ以外の場合は進む
183
+ }
184
+ else
185
+ {
186
+ transform.position += direction * speed;
187
+
188
+ //指定した時間を経過すると
189
+ if (timeCount > chargeTime)
190
+ {
191
+ //進路をランダムに変更する
192
+ direction = RandomDirection();
193
+ //TimeCountを0に戻す
194
+ timeCount = 0;
195
+ }
196
+
197
+ }
198
+ }
199
+
200
+ void Turn()
201
+ {
202
+ //タイムカウントを0に戻す。
203
+ timeCount = 0;
204
+
205
+ if (WorldPoint.y > maxMoveArea.y)
206
+ {
207
+ transform.position = new Vector3(transform.position.x, maxMoveArea.y-0.2f, transform.position.z);
208
+ direction = Vector3.down;
209
+ }
210
+ else if (WorldPoint.y < minMoveArea.y)
211
+ {
212
+ transform.position = new Vector3(transform.position.x, minMoveArea.y+0.2f, transform.position.z);
213
+ direction = Vector3.up;
214
+ }
215
+ else if (WorldPoint.x < minMoveArea.x)
216
+ {
217
+ transform.position = new Vector3(minMoveArea.x+0.2f, transform.position.y, transform.position.z);
218
+ direction = Vector3.right;
219
+ }
220
+ else if (WorldPoint.x > maxMoveArea.x)
221
+ {
222
+ transform.position = new Vector3(maxMoveArea.x, transform.position.y, transform.position.z);
223
+ direction = Vector3.left;
224
+ }
225
+ }
226
+
227
+ Vector3 RandomDirection()
228
+ {
229
+ int r = Random.Range(0, 4);
230
+ switch (r)
231
+ {
232
+ default:
233
+ case 0:
234
+ return Vector3.up;
235
+ case 1:
236
+ return Vector3.down;
237
+ case 2:
238
+ return Vector3.left;
239
+ case 3:
240
+ return Vector3.right;
241
+ }
242
+
243
+ }
244
+ }
245
+
123
246
  ```