質問編集履歴
1
文章とタイトルを編集しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
velocityで速度を取得したいが移動中も何故か値が0でたまに数値が変わるという謎の現象の理由と対処法が知りたい。
|
body
CHANGED
@@ -1,8 +1,17 @@
|
|
1
|
-
提示コードの
|
1
|
+
提示コード部のFixedUpdate()部の提示画像のデバッグログの右の何回同じものが出力されたかという数字の部分ですが何故か移動中ににもかかわらず**ログが(0,0,0)で移動中にたまに(2.0,0,0)など**といったおかしなログが出力されてしまうのでしょうか?**rb.AddForceの後にDebug.Log()を**載せているので直前の値が出力されるはずなのですが原因がわかりません。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
質問内容:ログの値がおかしい原因と理由が知りたい。
|
6
|
+
質問内容2:move.x *= 100などのいった値がmoveに入っていながらも2.0といった値もなぜこんなに小さい値なのかも謎ですこれは正しい可能性もありますが気になったので質問しました。
|
7
|
+
Unityリファレンス: https://docs.unity3d.com/ja/2018.4/ScriptReference/Rigidbody-velocity.html
|
5
8
|
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+

|
14
|
+
|
6
15
|
```ここに言語を入力
|
7
16
|
using System.Collections;
|
8
17
|
using System.Collections.Generic;
|
@@ -62,14 +71,20 @@
|
|
62
71
|
RaycastHit hit;
|
63
72
|
|
64
73
|
Vector3 forward = transform.forward + slope_range;
|
65
|
-
Debug.DrawLine(Slope.transform.position, Slope.transform.position + forward ,Color.blue);
|
74
|
+
Debug.DrawLine(Slope.transform.position, Slope.transform.position + forward ,Color.blue);
|
75
|
+
// float speed = Mathf.Sqrt((move.x * move.x) + (move.z * move.z));
|
76
|
+
float speed = Mathf.Sqrt((rb.velocity.x * rb.velocity.x) + (rb.velocity.z * rb.velocity.z));
|
66
77
|
|
78
|
+
if(speed != 0){
|
67
|
-
|
79
|
+
if(Physics.Linecast(Slope.transform.position, Slope.transform.position + forward, out hit,LayerMask.GetMask("Slope")) == true)
|
68
|
-
|
80
|
+
{
|
69
|
-
|
81
|
+
Debug.Log("坂道のRayヒット!");
|
70
|
-
|
82
|
+
|
71
|
-
|
83
|
+
v = new Vector3(0f, (Quaternion.FromToRotation(Vector3.up, hit.normal) * transform.forward * speed).y, 0f) + transform.forward * speed;
|
72
|
-
|
84
|
+
Debug.Log("v.y: "+ v.y);
|
85
|
+
}
|
86
|
+
}else{
|
87
|
+
v.y = 0;
|
73
88
|
}
|
74
89
|
|
75
90
|
}
|
@@ -80,26 +95,20 @@
|
|
80
95
|
|
81
96
|
input_h = Input.GetAxis("Horizontal");
|
82
97
|
input_v = Input.GetAxis("Vertical");
|
83
|
-
|
84
|
-
|
85
|
-
|
86
98
|
Vector3 move_z = new Vector3();
|
87
99
|
Vector3 move_x = new Vector3();
|
88
|
-
move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v
|
100
|
+
move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v;
|
89
|
-
move_x = Camera.main.transform.right * input_h;
|
101
|
+
move_x = Camera.main.transform.right * input_h;
|
90
102
|
move = move_x + move_z;
|
91
103
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
move.x *= 200;
|
96
|
-
move.z *= 200;
|
97
|
-
|
98
|
-
|
99
104
|
if (move != Vector3.zero)
|
100
105
|
{
|
101
106
|
transform.rotation = Quaternion.LookRotation(move.normalized);
|
102
107
|
}
|
108
|
+
|
109
|
+
move.x *= 100;
|
110
|
+
move.z *= 100;
|
111
|
+
|
103
112
|
}
|
104
113
|
|
105
114
|
// Update is called once per frame
|
@@ -110,18 +119,21 @@
|
|
110
119
|
Slope_Mng();
|
111
120
|
Move_Mng();
|
112
121
|
|
122
|
+
|
123
|
+
// Debug.Log("velocity: " + Mathf.Sqrt((rb.velocity.x * rb.velocity.x) + (rb.velocity.z * rb.velocity.z)));
|
124
|
+
|
113
125
|
}
|
114
126
|
|
115
|
-
|
127
|
+
|
116
128
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
117
129
|
void FixedUpdate()
|
118
130
|
{
|
119
131
|
|
120
|
-
// rb.velocity = new Vector3(move.x, 0, move.z);
|
121
132
|
|
122
133
|
rb.AddForce(move.x, 0, move.z);
|
134
|
+
Debug.Log("velocity: " + rb.velocity);
|
123
135
|
|
124
|
-
|
136
|
+
|
125
137
|
}
|
126
138
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
127
139
|
}
|