質問編集履歴
1
文章を編集しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Rayの線を常にプレイヤーの正面の方向から出す方法
|
1
|
+
Rayの線を常にプレイヤーの正面の方向から出し"長さを指定"する方法が知りたいです。
|
body
CHANGED
@@ -1,92 +1,29 @@
|
|
1
|
-
///////////ここのコードです。3Dアクションゲームなのですが、プレイヤーが左に向いたり前に向いたりしてプレイヤー自体が色んな方向に向いてもプレイヤーが正面の向きにRayを飛ば
|
1
|
+
///////////ここのコードです。3Dアクションゲームなのですが、プレイヤーが左に向いたり前に向いたりしてプレイヤー自体が色んな方向に向いてもプレイヤーが正面の向きにRayを飛ばし"Rayの線の長さを指定したい"です
|
2
|
+
用途は、Slope.transform.positionとはプレイヤーの足元に子のオブジェクト(Slope)を設定してそこからRayを飛ばして現在坂道に足元が居るかどうかを判定してY軸の増加量を計算しています。そのためにプレイヤーの足元からプレイヤーの正面方向に向かってRayを飛ばしたいのです。
|
2
3
|
|
4
|
+
質問内容は、つまりtransform.forwardでは長さが1の正面に居る場合は(0,0,1)の長さが来てしまうため長すぎです、自分は0.3,04といった長さを指定したいです。
|
3
5
|
|
4
|
-
```ここに言語を入力
|
5
6
|
|
6
|
-
using System.Collections;
|
7
|
-
using System.Collections.Generic;
|
8
|
-
using UnityEngine;
|
9
7
|
|
10
|
-
public class Controller : MonoBehaviour {
|
11
8
|
|
12
|
-
private float input_h;
|
13
|
-
private float input_v;
|
14
|
-
|
9
|
+
※Player操作スクリプトです。
|
15
|
-
|
10
|
+
```ここに言語を入力
|
16
|
-
|
11
|
+
void Slope_Mng()//坂道を上る時
|
17
|
-
private Animator ani;//移動速度
|
18
|
-
private Vector3 gravity;
|
19
|
-
private bool isJump = false;//ジャンプしてるかどうかを管理する
|
20
|
-
private Vector3 v;
|
21
|
-
private bool isDush;
|
22
|
-
private bool isGround;
|
23
|
-
|
24
|
-
private bool isAttack;
|
25
|
-
|
26
|
-
GameObject Ground;
|
27
|
-
GameObject Slope;
|
28
|
-
|
29
|
-
Ground ground_sct;
|
30
|
-
|
31
|
-
//public GameObject effect;
|
32
|
-
|
33
|
-
// Use this for initialization
|
34
|
-
void Start () {
|
35
|
-
|
36
|
-
Ground = GameObject.Find("GroundCheck");
|
37
|
-
Slope = GameObject.Find("SlopeCheck");
|
38
|
-
|
39
|
-
isGround = true;
|
40
|
-
|
41
|
-
isDush = false;
|
42
|
-
isAttack = false;
|
43
|
-
walk_speed = 10.0f;
|
44
|
-
gravity = Vector3.zero;
|
45
|
-
rb = GetComponent<Rigidbody>();
|
46
|
-
ani = GetComponent<Animator>();
|
47
|
-
|
48
|
-
|
49
|
-
}
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
void Animation_Mng()
|
56
12
|
{
|
13
|
+
Vector3 range = new Vector3(0, -0.3f, -0.7f);
|
14
|
+
// Debug.DrawLine(Slope.transform.position, Slope.transform.position + transform.forward + new Vector3(0, -0.3f, 0.3f), Color.blue);
|
57
|
-
|
15
|
+
Debug.DrawLine(Slope.transform.position, Slope.transform.position + (transform.forward), Color.blue);
|
16
|
+
//Debug.DrawLine(Slope.transform.position, Slope.transform.position + new Vector3(0,-1,1) , Color.blue);
|
58
17
|
|
59
|
-
|
18
|
+
Debug.Log("forward" + transform.forward);
|
60
19
|
|
61
|
-
ani.SetBool("Dush", isDush);
|
62
|
-
|
63
|
-
|
64
|
-
}
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
void Slope_Mng()//坂道を上る時
|
69
|
-
{
|
70
|
-
//////////////////////////////////////////////////////ここのコード
|
71
|
-
Debug.DrawLine(Slope.transform.position, Slope.transform.position + new Vector3(0, -0.3f, 0.3f), Color.blue);
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
if (move != Vector3.zero)
|
76
|
-
{
|
77
|
-
Slope.transform.rotation = transform.rotation;
|
78
|
-
|
79
|
-
Debug.Log("slope rotation: " + Slope.transform.rotation);
|
80
|
-
}
|
81
|
-
|
82
|
-
|
83
|
-
|
84
20
|
//Transform hit;
|
85
21
|
RaycastHit hit;
|
22
|
+
|
86
23
|
if(move.x != 0 || move.z != 0){
|
87
24
|
//Debug.DrawLine(Slope.transform.position,Slope.transform.position + new Vector3(0,-1f,1f),Color.blue);
|
88
25
|
|
89
|
-
if(Physics.Linecast(Slope.transform.position,
|
26
|
+
if(Physics.Linecast(Slope.transform.position, transform.position + new Vector3(0, -0.3f, 0.3f),out hit,LayerMask.GetMask("Slope")) == true)
|
90
27
|
{
|
91
28
|
Debug.Log("Slope ");
|
92
29
|
|
@@ -105,108 +42,6 @@
|
|
105
42
|
Debug.Log("y: " + v);
|
106
43
|
}
|
107
44
|
|
108
|
-
|
109
45
|
}
|
110
46
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
void Gravity_Mng()//ジャンプ管理
|
117
|
-
{
|
118
|
-
//float range = -1f;
|
119
|
-
|
120
|
-
Debug.DrawLine(Ground.transform.position, Ground.transform.position + new Vector3(0, -0.2f, 0), Color.red);
|
121
|
-
|
122
|
-
if (Physics.Raycast(Ground.transform.position, new Vector3(0, -1, 0), 0.3f) == true)
|
123
|
-
{
|
124
|
-
|
125
|
-
isGround = true;
|
126
|
-
gravity.y = 0;
|
127
|
-
|
128
|
-
Debug.Log("地面です。");
|
129
|
-
|
130
|
-
}
|
131
|
-
|
132
|
-
|
133
|
-
if (Input.GetKeyDown("space") == true && isGround == true)
|
134
|
-
{
|
135
|
-
//gravity.y = 80;//ジャンプ力
|
136
|
-
gravity.y = 80;//ジャンプ力
|
137
|
-
|
138
|
-
Debug.Log("ジャンプ");
|
139
|
-
isGround = false;
|
140
|
-
}
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
}
|
145
|
-
|
146
|
-
|
147
|
-
void Attack_Mng()//攻撃管理
|
148
|
-
{
|
149
|
-
if(Input.GetMouseButtonDown(0) == true)
|
150
|
-
{
|
151
|
-
Debug.Log("左クリック(攻撃)");
|
152
|
-
// Instantiate(effect);
|
153
|
-
ani.SetBool("isAttack",true);
|
154
|
-
}else{
|
155
|
-
ani.SetBool("isAttack", false);
|
156
|
-
}
|
157
|
-
}
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
void Move_Mng()
|
162
|
-
{
|
163
|
-
|
164
|
-
input_h = Input.GetAxis("Horizontal");
|
165
|
-
input_v = Input.GetAxis("Vertical");
|
166
|
-
if(Input.GetKey(KeyCode.LeftShift) == true)
|
167
|
-
{
|
168
|
-
Debug.Log("Shift Dush ");
|
169
|
-
isDush = true;
|
170
|
-
}else{
|
171
|
-
isDush = false;
|
172
|
-
}
|
173
|
-
|
174
|
-
|
175
|
-
Vector3 move_z = new Vector3();
|
176
|
-
Vector3 move_x = new Vector3();
|
177
|
-
|
178
|
-
move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v * walk_speed;//Z
|
179
|
-
move_x = Camera.main.transform.right * input_h * walk_speed;//X
|
180
|
-
move = move_x + move_z;
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
//角度を触るので
|
186
|
-
//Vector3 d = move;
|
187
|
-
if (move != Vector3.zero)
|
188
|
-
{
|
189
|
-
transform.rotation = Quaternion.LookRotation(move.normalized);
|
190
|
-
}
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
}
|
195
|
-
|
196
|
-
// Update is called once per frame
|
197
|
-
void Update () {
|
198
|
-
Animation_Mng();
|
199
|
-
Gravity_Mng();
|
200
|
-
Attack_Mng();
|
201
|
-
Move_Mng();
|
202
|
-
Slope_Mng();
|
203
|
-
|
204
|
-
|
205
|
-
rb.velocity = new Vector3(rb.velocity.x,v.y, rb.velocity.z);
|
206
|
-
v.y = 0;
|
207
|
-
|
208
|
-
// Debug.Log(rb.velocity);
|
209
|
-
|
210
|
-
}
|
211
|
-
|
212
47
|
```
|