質問編集履歴
1
文章を編集しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Rayの線を常にプレイヤーの正面の方向から出す方法
|
1
|
+
Rayの線を常にプレイヤーの正面の方向から出し"長さを指定"する方法が知りたいです。
|
test
CHANGED
@@ -1,100 +1,10 @@
|
|
1
|
-
///////////ここのコードです。3Dアクションゲームなのですが、プレイヤーが左に向いたり前に向いたりしてプレイヤー自体が色んな方向に向いてもプレイヤーが正面の向きにRayを飛ば
|
1
|
+
///////////ここのコードです。3Dアクションゲームなのですが、プレイヤーが左に向いたり前に向いたりしてプレイヤー自体が色んな方向に向いてもプレイヤーが正面の向きにRayを飛ばし"Rayの線の長さを指定したい"です
|
2
|
+
|
3
|
+
用途は、Slope.transform.positionとはプレイヤーの足元に子のオブジェクト(Slope)を設定してそこからRayを飛ばして現在坂道に足元が居るかどうかを判定してY軸の増加量を計算しています。そのためにプレイヤーの足元からプレイヤーの正面方向に向かってRayを飛ばしたいのです。
|
2
4
|
|
3
5
|
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
```ここに言語を入力
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
using System.Collections;
|
12
|
-
|
13
|
-
using System.Collections.Generic;
|
14
|
-
|
15
|
-
using UnityEngine;
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
public class Controller : MonoBehaviour {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
private float input_h;
|
24
|
-
|
25
|
-
private float input_v;
|
26
|
-
|
27
|
-
private Vector3 move;
|
28
|
-
|
29
|
-
private Rigidbody rb;
|
30
|
-
|
31
|
-
private float walk_speed;//移動速度
|
32
|
-
|
33
|
-
private Animator ani;//移動速度
|
34
|
-
|
35
|
-
private Vector3 gravity;
|
36
|
-
|
37
|
-
|
7
|
+
質問内容は、つまりtransform.forwardでは長さが1の正面に居る場合は(0,0,1)の長さが来てしまうため長すぎです、自分は0.3,04といった長さを指定したいです。
|
38
|
-
|
39
|
-
private Vector3 v;
|
40
|
-
|
41
|
-
private bool isDush;
|
42
|
-
|
43
|
-
private bool isGround;
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
private bool isAttack;
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
GameObject Ground;
|
52
|
-
|
53
|
-
GameObject Slope;
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
Ground ground_sct;
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
//public GameObject effect;
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
// Use this for initialization
|
66
|
-
|
67
|
-
void Start () {
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
Ground = GameObject.Find("GroundCheck");
|
72
|
-
|
73
|
-
Slope = GameObject.Find("SlopeCheck");
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
isGround = true;
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
isDush = false;
|
82
|
-
|
83
|
-
isAttack = false;
|
84
|
-
|
85
|
-
walk_speed = 10.0f;
|
86
|
-
|
87
|
-
gravity = Vector3.zero;
|
88
|
-
|
89
|
-
rb = GetComponent<Rigidbody>();
|
90
|
-
|
91
|
-
ani = GetComponent<Animator>();
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
}
|
98
8
|
|
99
9
|
|
100
10
|
|
@@ -104,63 +14,25 @@
|
|
104
14
|
|
105
15
|
|
106
16
|
|
107
|
-
|
17
|
+
※Player操作スクリプトです。
|
108
18
|
|
19
|
+
```ここに言語を入力
|
20
|
+
|
109
|
-
|
21
|
+
void Slope_Mng()//坂道を上る時
|
110
22
|
|
111
23
|
{
|
112
24
|
|
25
|
+
Vector3 range = new Vector3(0, -0.3f, -0.7f);
|
26
|
+
|
27
|
+
// Debug.DrawLine(Slope.transform.position, Slope.transform.position + transform.forward + new Vector3(0, -0.3f, 0.3f), Color.blue);
|
28
|
+
|
113
|
-
|
29
|
+
Debug.DrawLine(Slope.transform.position, Slope.transform.position + (transform.forward), Color.blue);
|
30
|
+
|
31
|
+
//Debug.DrawLine(Slope.transform.position, Slope.transform.position + new Vector3(0,-1,1) , Color.blue);
|
114
32
|
|
115
33
|
|
116
34
|
|
117
|
-
ani.SetFloat("Forward", f);
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
ani.SetBool("Dush", isDush);
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
void Slope_Mng()//坂道を上る時
|
136
|
-
|
137
|
-
{
|
138
|
-
|
139
|
-
//////////////////////////////////////////////////////ここのコード
|
140
|
-
|
141
|
-
Debug.DrawLine(Slope.transform.position, Slope.transform.position + new Vector3(0, -0.3f, 0.3f), Color.blue);
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
if (move != Vector3.zero)
|
150
|
-
|
151
|
-
{
|
152
|
-
|
153
|
-
Slope.transform.rotation = transform.rotation;
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
Debug.Log("
|
35
|
+
Debug.Log("forward" + transform.forward);
|
158
|
-
|
159
|
-
}
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
36
|
|
165
37
|
|
166
38
|
|
@@ -168,13 +40,15 @@
|
|
168
40
|
|
169
41
|
RaycastHit hit;
|
170
42
|
|
43
|
+
|
44
|
+
|
171
45
|
if(move.x != 0 || move.z != 0){
|
172
46
|
|
173
47
|
//Debug.DrawLine(Slope.transform.position,Slope.transform.position + new Vector3(0,-1f,1f),Color.blue);
|
174
48
|
|
175
49
|
|
176
50
|
|
177
|
-
if(Physics.Linecast(Slope.transform.position,
|
51
|
+
if(Physics.Linecast(Slope.transform.position, transform.position + new Vector3(0, -0.3f, 0.3f),out hit,LayerMask.GetMask("Slope")) == true)
|
178
52
|
|
179
53
|
{
|
180
54
|
|
@@ -212,210 +86,6 @@
|
|
212
86
|
|
213
87
|
|
214
88
|
|
215
|
-
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
void Gravity_Mng()//ジャンプ管理
|
232
|
-
|
233
|
-
{
|
234
|
-
|
235
|
-
//float range = -1f;
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
Debug.DrawLine(Ground.transform.position, Ground.transform.position + new Vector3(0, -0.2f, 0), Color.red);
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
if (Physics.Raycast(Ground.transform.position, new Vector3(0, -1, 0), 0.3f) == true)
|
244
|
-
|
245
|
-
{
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
isGround = true;
|
250
|
-
|
251
|
-
gravity.y = 0;
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
Debug.Log("地面です。");
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
}
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
if (Input.GetKeyDown("space") == true && isGround == true)
|
266
|
-
|
267
|
-
{
|
268
|
-
|
269
|
-
//gravity.y = 80;//ジャンプ力
|
270
|
-
|
271
|
-
gravity.y = 80;//ジャンプ力
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
Debug.Log("ジャンプ");
|
276
|
-
|
277
|
-
isGround = false;
|
278
|
-
|
279
|
-
}
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
}
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
void Attack_Mng()//攻撃管理
|
294
|
-
|
295
|
-
{
|
296
|
-
|
297
|
-
if(Input.GetMouseButtonDown(0) == true)
|
298
|
-
|
299
|
-
{
|
300
|
-
|
301
|
-
Debug.Log("左クリック(攻撃)");
|
302
|
-
|
303
|
-
// Instantiate(effect);
|
304
|
-
|
305
|
-
ani.SetBool("isAttack",true);
|
306
|
-
|
307
|
-
}else{
|
308
|
-
|
309
|
-
ani.SetBool("isAttack", false);
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
void Move_Mng()
|
322
|
-
|
323
|
-
{
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
input_h = Input.GetAxis("Horizontal");
|
328
|
-
|
329
|
-
input_v = Input.GetAxis("Vertical");
|
330
|
-
|
331
|
-
if(Input.GetKey(KeyCode.LeftShift) == true)
|
332
|
-
|
333
|
-
{
|
334
|
-
|
335
|
-
Debug.Log("Shift Dush ");
|
336
|
-
|
337
|
-
isDush = true;
|
338
|
-
|
339
|
-
}else{
|
340
|
-
|
341
|
-
isDush = false;
|
342
|
-
|
343
|
-
}
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
Vector3 move_z = new Vector3();
|
350
|
-
|
351
|
-
Vector3 move_x = new Vector3();
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v * walk_speed;//Z
|
356
|
-
|
357
|
-
move_x = Camera.main.transform.right * input_h * walk_speed;//X
|
358
|
-
|
359
|
-
move = move_x + move_z;
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
//角度を触るので
|
370
|
-
|
371
|
-
//Vector3 d = move;
|
372
|
-
|
373
|
-
if (move != Vector3.zero)
|
374
|
-
|
375
|
-
{
|
376
|
-
|
377
|
-
transform.rotation = Quaternion.LookRotation(move.normalized);
|
378
|
-
|
379
|
-
}
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
}
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
// Update is called once per frame
|
392
|
-
|
393
|
-
void Update () {
|
394
|
-
|
395
|
-
Animation_Mng();
|
396
|
-
|
397
|
-
Gravity_Mng();
|
398
|
-
|
399
|
-
Attack_Mng();
|
400
|
-
|
401
|
-
Move_Mng();
|
402
|
-
|
403
|
-
Slope_Mng();
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
rb.velocity = new Vector3(rb.velocity.x,v.y, rb.velocity.z);
|
410
|
-
|
411
|
-
v.y = 0;
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
// Debug.Log(rb.velocity);
|
416
|
-
|
417
|
-
|
418
|
-
|
419
89
|
}
|
420
90
|
|
421
91
|
|