質問編集履歴

1

追記コード

2021/06/27 01:35

投稿

momonoki
momonoki

スコア21

test CHANGED
File without changes
test CHANGED
@@ -243,3 +243,249 @@
243
243
 
244
244
 
245
245
  ```
246
+
247
+
248
+
249
+ 追記コード
250
+
251
+ ```
252
+
253
+ using System.Collections;
254
+
255
+ using System.Collections.Generic;
256
+
257
+ using UnityEngine;
258
+
259
+ using UnityEngine.UI;
260
+
261
+
262
+
263
+ public class crowdHuman : MonoBehaviour
264
+
265
+ {
266
+
267
+ private float chargeTime;//動く時間
268
+
269
+ private float timeCount;
270
+
271
+ private float speed;//移動スピード
272
+
273
+ private float speed_Min;//Minスピード
274
+
275
+ private float speed_Max;//Maxスピード
276
+
277
+ Vector3 direction;//移動方向
278
+
279
+ Vector3 pos;//自分の位置
280
+
281
+ Vector3 minMoveArea = new Vector3(-8.9f, -4.9f, 0);//移動範囲(最小)
282
+
283
+ Vector3 maxMoveArea = new Vector3(-4.9f, 4.9f, 0);//移動範囲(最大)
284
+
285
+ Vector3 WorldPoint;
286
+
287
+ public Sprite[] HumanSprites;
288
+
289
+ Sprite HumanSprite;
290
+
291
+
292
+
293
+ // Start is called before the first frame update
294
+
295
+ void Start()
296
+
297
+ {
298
+
299
+ chargeTime = 0.9f;
300
+
301
+
302
+
303
+ //開始時の移動方向を決める
304
+
305
+ direction = RandomDirection();
306
+
307
+
308
+
309
+ //移動スピードをランダムに決める
310
+
311
+ speed_Min = 0.01f;
312
+
313
+ speed_Max = 0.02f;
314
+
315
+ speed = Random.Range(speed_Min, speed_Max);
316
+
317
+
318
+
319
+ //自分の位置を取得
320
+
321
+ Transform myTransform = this.transform;
322
+
323
+ pos = myTransform.position;
324
+
325
+
326
+
327
+ HumanSprites = Resources.LoadAll<Sprite>("image");
328
+
329
+ int i = Random.Range(0, 3);
330
+
331
+ HumanSprite = HumanSprites[i];
332
+
333
+ this.GetComponent<Image>().sprite = HumanSprite;
334
+
335
+ }
336
+
337
+
338
+
339
+ // Update is called once per frame
340
+
341
+ void Update()
342
+
343
+ {
344
+
345
+ timeCount += Time.deltaTime;
346
+
347
+
348
+
349
+ WorldPoint = transform.TransformPoint(pos);
350
+
351
+
352
+
353
+ //枠から出たら逆方向を向く
354
+
355
+ //if ((pos.x < minMoveArea.x)||(pos.y < minMoveArea.y)||(pos.x > maxMoveArea.x)||(pos.y > maxMoveArea.y))
356
+
357
+ if ((WorldPoint.x < minMoveArea.x) || (WorldPoint.y < minMoveArea.y) || (WorldPoint.x > maxMoveArea.x) || (WorldPoint.y > maxMoveArea.y))
358
+
359
+ {
360
+
361
+ Turn();
362
+
363
+ //それ以外の場合は進む
364
+
365
+ }
366
+
367
+ else
368
+
369
+ {
370
+
371
+ transform.position += direction * speed;
372
+
373
+
374
+
375
+ //指定した時間を経過すると
376
+
377
+ if (timeCount > chargeTime)
378
+
379
+ {
380
+
381
+ //進路をランダムに変更する
382
+
383
+ direction = RandomDirection();
384
+
385
+ //TimeCountを0に戻す
386
+
387
+ timeCount = 0;
388
+
389
+ }
390
+
391
+
392
+
393
+ }
394
+
395
+ }
396
+
397
+
398
+
399
+ void Turn()
400
+
401
+ {
402
+
403
+ //タイムカウントを0に戻す。
404
+
405
+ timeCount = 0;
406
+
407
+
408
+
409
+ if (WorldPoint.y > maxMoveArea.y)
410
+
411
+ {
412
+
413
+ transform.position = new Vector3(transform.position.x, maxMoveArea.y-0.2f, transform.position.z);
414
+
415
+ direction = Vector3.down;
416
+
417
+ }
418
+
419
+ else if (WorldPoint.y < minMoveArea.y)
420
+
421
+ {
422
+
423
+ transform.position = new Vector3(transform.position.x, minMoveArea.y+0.2f, transform.position.z);
424
+
425
+ direction = Vector3.up;
426
+
427
+ }
428
+
429
+ else if (WorldPoint.x < minMoveArea.x)
430
+
431
+ {
432
+
433
+ transform.position = new Vector3(minMoveArea.x+0.2f, transform.position.y, transform.position.z);
434
+
435
+ direction = Vector3.right;
436
+
437
+ }
438
+
439
+ else if (WorldPoint.x > maxMoveArea.x)
440
+
441
+ {
442
+
443
+ transform.position = new Vector3(maxMoveArea.x, transform.position.y, transform.position.z);
444
+
445
+ direction = Vector3.left;
446
+
447
+ }
448
+
449
+ }
450
+
451
+
452
+
453
+ Vector3 RandomDirection()
454
+
455
+ {
456
+
457
+ int r = Random.Range(0, 4);
458
+
459
+ switch (r)
460
+
461
+ {
462
+
463
+ default:
464
+
465
+ case 0:
466
+
467
+ return Vector3.up;
468
+
469
+ case 1:
470
+
471
+ return Vector3.down;
472
+
473
+ case 2:
474
+
475
+ return Vector3.left;
476
+
477
+ case 3:
478
+
479
+ return Vector3.right;
480
+
481
+ }
482
+
483
+
484
+
485
+ }
486
+
487
+ }
488
+
489
+
490
+
491
+ ```