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

質問編集履歴

1

ソース一部変更とその他

2017/01/05 15:56

投稿

TKM_waiwai
TKM_waiwai

スコア16

title CHANGED
File without changes
body CHANGED
@@ -10,20 +10,30 @@
10
10
  どうしたらランダムで巡回してくれるでしょうか。
11
11
  現状のEnemyソースに間違いや足りない箇所などがあればアドバイスよろしくお願い致します。
12
12
 
13
+ 【巡回AIの参考URL】
14
+
15
+ http://tongullman.blogspot.jp/2015/11/unity-navmesh-ai.html
16
+
17
+ 【編集】
18
+ ソースを一部書き直しました。
19
+ 参考にしたURLを貼りました。
20
+
21
+ 質問の仕方が下手で申し訳ありません。
22
+
13
23
  ```C#
14
24
  using UnityEngine;
15
25
  using System.Collections;
16
26
 
17
27
  public class Enemy : MonoBehaviour
18
28
  {
19
- public float m_speed; //移動速度
29
+ public float m_speed = 2.0f; //移動速度
20
30
  public float m_rotation; //回転速度
21
- public Transform m_target; //対象オブジェクト
31
+ public Transform m_target; //追いかけるオブジェクト
22
32
  NavMeshAgent m_agent;
23
33
 
24
34
  private static Vector3 m_position;
25
- private float agentToPatroldistance;
35
+ private float m_patroldistance;
26
- private float agentToTargetdistance;
36
+ private float m_targetdistance;
27
37
 
28
38
  void Awake()
29
39
  {
@@ -37,16 +47,19 @@
37
47
 
38
48
  void Update()
39
49
  {
40
- agentToPatroldistance = Vector3.Distance(this.m_agent.transform.position,m_position);
50
+ m_patroldistance = Vector3.Distance(this.m_agent.transform.position,m_position);
41
- agentToTargetdistance = Vector3.Distance(this.m_agent.transform.position, m_target.transform.position);
51
+ m_targetdistance = Vector3.Distance(this.m_agent.transform.position, m_target.transform.position);
42
52
 
43
53
  //Playerとの距離が30f以下になると逃げる
44
- if(agentToTargetdistance <= 30f)
54
+ if(m_targetdistance <= 30f)
45
55
  {
46
56
  Move();
47
- }else if(agentToPatroldistance < 15f)
57
+ }else if(m_patroldistance < 15f)
48
58
  {
49
59
  DoPatrol();
60
+ }else
61
+ {
62
+ m_agent.SetDestination(m_position);
50
63
  }
51
64
  }
52
65