質問編集履歴

1

ソース一部変更とその他

2017/01/05 15:56

投稿

TKM_waiwai
TKM_waiwai

スコア16

test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,26 @@
22
22
 
23
23
 
24
24
 
25
+ 【巡回AIの参考URL】
26
+
27
+
28
+
29
+ http://tongullman.blogspot.jp/2015/11/unity-navmesh-ai.html
30
+
31
+
32
+
33
+ 【編集】
34
+
35
+ ソースを一部書き直しました。
36
+
37
+ 参考にしたURLを貼りました。
38
+
39
+
40
+
41
+ 質問の仕方が下手で申し訳ありません。
42
+
43
+
44
+
25
45
  ```C#
26
46
 
27
47
  using UnityEngine;
@@ -34,11 +54,11 @@
34
54
 
35
55
  {
36
56
 
37
- public float m_speed; //移動速度
57
+ public float m_speed = 2.0f; //移動速度
38
58
 
39
59
  public float m_rotation; //回転速度
40
60
 
41
- public Transform m_target; //対象オブジェクト
61
+ public Transform m_target; //追いかけるオブジェクト
42
62
 
43
63
  NavMeshAgent m_agent;
44
64
 
@@ -46,9 +66,9 @@
46
66
 
47
67
  private static Vector3 m_position;
48
68
 
49
- private float agentToPatroldistance;
69
+ private float m_patroldistance;
50
70
 
51
- private float agentToTargetdistance;
71
+ private float m_targetdistance;
52
72
 
53
73
 
54
74
 
@@ -76,25 +96,31 @@
76
96
 
77
97
  {
78
98
 
79
- agentToPatroldistance = Vector3.Distance(this.m_agent.transform.position,m_position);
99
+ m_patroldistance = Vector3.Distance(this.m_agent.transform.position,m_position);
80
100
 
81
- agentToTargetdistance = Vector3.Distance(this.m_agent.transform.position, m_target.transform.position);
101
+ m_targetdistance = Vector3.Distance(this.m_agent.transform.position, m_target.transform.position);
82
102
 
83
103
 
84
104
 
85
105
  //Playerとの距離が30f以下になると逃げる
86
106
 
87
- if(agentToTargetdistance <= 30f)
107
+ if(m_targetdistance <= 30f)
88
108
 
89
109
  {
90
110
 
91
111
  Move();
92
112
 
93
- }else if(agentToPatroldistance < 15f)
113
+ }else if(m_patroldistance < 15f)
94
114
 
95
115
  {
96
116
 
97
117
  DoPatrol();
118
+
119
+ }else
120
+
121
+ {
122
+
123
+ m_agent.SetDestination(m_position);
98
124
 
99
125
  }
100
126