質問編集履歴

1

Player.csを追加

2018/05/29 14:57

投稿

mikaduki444
mikaduki444

スコア6

test CHANGED
File without changes
test CHANGED
@@ -92,6 +92,204 @@
92
92
 
93
93
  ```
94
94
 
95
+ ### Player.cs
96
+
97
+ ```C#
98
+
99
+ using System.Collections;
100
+
101
+ using System.Collections.Generic;
102
+
103
+ using UnityEngine;
104
+
105
+
106
+
107
+ public class Player: MonoBehaviour
108
+
109
+ {
110
+
111
+ [SerializeField] Vector3 velocity; //移動方向
112
+
113
+ public static float speed = 2.0f; //移動速度
114
+
115
+ [SerializeField] float rotation = 0.2f; //回転速度
116
+
117
+ public static Animator animator;
118
+
119
+
120
+
121
+ void Start()
122
+
123
+ {
124
+
125
+ animator = GetComponent<Animator>();
126
+
127
+ }
128
+
129
+
130
+
131
+ void Update()
132
+
133
+ {
134
+
135
+ velocity = Vector3.zero;
136
+
137
+
138
+
139
+ if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.UpArrow))
140
+
141
+ {
142
+
143
+ animator.SetBool("Walking", true);
144
+
145
+ if (!ForestSound.footsteps.isPlaying) { ForestSound.footsteps.PlayOneShot(ForestSound.footsteps.clip); }
146
+
147
+
148
+
149
+ if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
150
+
151
+ {
152
+
153
+ speed = 5.0f;
154
+
155
+ if (!ForestSound.footsteps.isPlaying) { ForestSound.footsteps.PlayOneShot(ForestSound.footsteps.clip); }
156
+
157
+ animator.SetBool("Walking", false);
158
+
159
+ animator.SetBool("Running", true);
160
+
161
+ }
162
+
163
+ if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
164
+
165
+ {
166
+
167
+ speed = 2.0f;
168
+
169
+ if (ForestSound.footsteps.isPlaying) { ForestSound.footsteps.Stop(); }
170
+
171
+ animator.SetBool("Running", false);
172
+
173
+ animator.SetBool("Walking", true);
174
+
175
+ }
176
+
177
+ }
178
+
179
+ else
180
+
181
+ {
182
+
183
+ animator.SetBool("Walking", false);
184
+
185
+ animator.SetBool("Running", false);
186
+
187
+ if (ForestSound.footsteps.isPlaying) { ForestSound.footsteps.Stop(); }
188
+
189
+ }
190
+
191
+
192
+
193
+ if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
194
+
195
+ {
196
+
197
+ //移動(X軸、Y軸、Z軸)
198
+
199
+ velocity.x += 1;
200
+
201
+ }
202
+
203
+ if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
204
+
205
+ {
206
+
207
+ velocity.x -= 1;
208
+
209
+ }
210
+
211
+ if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
212
+
213
+ {
214
+
215
+ velocity.z += 1;
216
+
217
+ }
218
+
219
+ if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
220
+
221
+ {
222
+
223
+ velocity.z -= 1;
224
+
225
+ }
226
+
227
+ if ((Input.GetKey(KeyCode.W)&& Input.GetKey(KeyCode.A)) || (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.LeftArrow)))
228
+
229
+ {
230
+
231
+ velocity.x -= 1;
232
+
233
+ velocity.z += 1;
234
+
235
+ }
236
+
237
+ if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) || (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow)))
238
+
239
+ {
240
+
241
+ velocity.x += 1;
242
+
243
+ velocity.z += 1;
244
+
245
+ }
246
+
247
+ if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A) || (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow)))
248
+
249
+ {
250
+
251
+ velocity.x -= 1;
252
+
253
+ velocity.z -= 1;
254
+
255
+ }
256
+
257
+ if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D) || (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow)))
258
+
259
+ {
260
+
261
+ velocity.x += 1;
262
+
263
+ velocity.z -= 1;
264
+
265
+ }
266
+
267
+
268
+
269
+ velocity = velocity.normalized * speed * Time.deltaTime;
270
+
271
+ if(velocity.magnitude > 0)
272
+
273
+ {
274
+
275
+ transform.position += velocity;
276
+
277
+ transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(velocity), rotation);
278
+
279
+ }
280
+
281
+
282
+
283
+ }
284
+
285
+
286
+
287
+ }
288
+
289
+
290
+
291
+ ```
292
+
95
293
 
96
294
 
97
295
  ### 試したこと