質問編集履歴

1

スクリプトの全体像を追記

2020/10/19 12:39

投稿

CraftTable
CraftTable

スコア234

test CHANGED
File without changes
test CHANGED
@@ -35,3 +35,225 @@
35
35
  ```
36
36
 
37
37
  お願いします
38
+
39
+
40
+
41
+ 追記
42
+
43
+ スクリプト全てが見たいとのことでしたのでここに貼ります
44
+
45
+ Target = 標的(プレイヤー)
46
+
47
+ WalkTargetPoint = NavMeshAgentで取得した向きを入れるVector3型変数
48
+
49
+
50
+
51
+ ```C#
52
+
53
+ public class Enemy1Script : MonoBehaviour
54
+
55
+ {
56
+
57
+ [SerializeField]
58
+
59
+ NavMeshAgent nav;
60
+
61
+ public GameObject Target;
62
+
63
+ [SerializeField]
64
+
65
+ Rigidbody rb;
66
+
67
+ Vector3 WalkTargetPoint;
68
+
69
+ [SerializeField]
70
+
71
+ Animator anim;
72
+
73
+ [SerializeField]
74
+
75
+ float LimitSpeed;
76
+
77
+ [SerializeField]
78
+
79
+ float MaxSpeed;
80
+
81
+ [SerializeField]
82
+
83
+ float MinSpeed;
84
+
85
+ [SerializeField]
86
+
87
+ float HP;
88
+
89
+ [SerializeField]
90
+
91
+ float speed;
92
+
93
+ // Start is called before the first frame update
94
+
95
+ void Start()
96
+
97
+ {
98
+
99
+ Target = GameObject.Find("Player");
100
+
101
+ HP = 100;
102
+
103
+ }
104
+
105
+
106
+
107
+ // Update is called once per frame
108
+
109
+ void Update()
110
+
111
+ {
112
+
113
+ speed = rb.velocity.magnitude;
114
+
115
+ nav.SetDestination(Target.transform.position);
116
+
117
+ if (nav.pathPending == false)
118
+
119
+ {
120
+
121
+ WalkTargetPoint = nav.path.corners[1];
122
+
123
+ Quaternion Point = Quaternion.LookRotation(WalkTargetPoint - transform.position);
124
+
125
+ Point.x = 0;
126
+
127
+ Point.z = 0;
128
+
129
+ transform.rotation = Point;
130
+
131
+ if (Vector3.Distance(transform.position, Target.transform.position) >= 30 && rb.velocity.magnitude <= LimitSpeed)
132
+
133
+ {
134
+
135
+ rb.AddForce(transform.forward / Random.Range(MaxSpeed, MinSpeed), ForceMode.Impulse);
136
+
137
+ anim.SetBool("Run", true);
138
+
139
+ }
140
+
141
+ else if(Vector3.Distance(transform.position, Target.transform.position) <= 5 && rb.velocity.magnitude <= LimitSpeed)
142
+
143
+ {
144
+
145
+ rb.AddForce(-transform.forward / Random.Range(MaxSpeed, MinSpeed), ForceMode.Impulse);
146
+
147
+ anim.SetBool("Run", true);
148
+
149
+ }
150
+
151
+ else
152
+
153
+ {
154
+
155
+ anim.SetBool("Run", false);
156
+
157
+ anim.SetTrigger("1Attack");
158
+
159
+ }
160
+
161
+
162
+
163
+ }
164
+
165
+ if (HP <= 0)
166
+
167
+ {
168
+
169
+ Destroy(gameObject);
170
+
171
+ }
172
+
173
+ }
174
+
175
+
176
+
177
+ void OnTriggerStay(Collider other)
178
+
179
+ {
180
+
181
+ if (other.gameObject.tag == "Hydrochloric Acid")
182
+
183
+ {
184
+
185
+ HP -= 0.01f;
186
+
187
+ rb.velocity = new Vector3(rb.velocity.x / 1.0005f, rb.velocity.y / 1.0005f, rb.velocity.z / 1.0005f);
188
+
189
+ }
190
+
191
+ }
192
+
193
+ void OnTriggerEnter(Collider other)
194
+
195
+ {
196
+
197
+ if (other.gameObject.tag == "Atack")
198
+
199
+ {
200
+
201
+ HP -= 10;
202
+
203
+ rb.AddForce(transform.forward * -100, ForceMode.Impulse);
204
+
205
+ }
206
+
207
+ }
208
+
209
+ void OnCollisionEnter(Collision collision)
210
+
211
+ {
212
+
213
+ if (collision.gameObject.tag == "Hydrochloric Acid")
214
+
215
+ {
216
+
217
+ if (collision.gameObject.name == "LastRE-ZA-")
218
+
219
+ {
220
+
221
+ HP -= 50;
222
+
223
+ }
224
+
225
+ else
226
+
227
+ {
228
+
229
+ HP -= 2;
230
+
231
+ }
232
+
233
+ }
234
+
235
+ }
236
+
237
+ void OnCollisionStay(Collision collision)
238
+
239
+ {
240
+
241
+ if (collision.gameObject.tag == "Hydrochloric Acid")
242
+
243
+ {
244
+
245
+ HP -= 5;
246
+
247
+ }
248
+
249
+ }
250
+
251
+ }
252
+
253
+ ```
254
+
255
+ このスクリプトだとrb.velocity.magnitudeが際限なく増え続けます(speedにmagnitudeを入れて表示してるのですがどこまでも増え続けてるのにオブジェクトは動きません)
256
+
257
+ 無理だったらプロジェクト作り直しします
258
+
259
+ おねがいしますm(_ _)m