質問編集履歴

1

文章とタイトルを編集しました。

2020/02/14 11:30

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- 数式計算に使う移動速度が知りたい。この場合どれを使えばいいのか
1
+ velocityで速度を取得したい移動中も何故か値が0でたまに数値が変わるという謎の現象の理由と対処法が知りたい。
test CHANGED
@@ -1,10 +1,28 @@
1
- 提示コードのSlope_Mng()関数/////コメント一行コードですwalk_speedという移動速度を使ってY軸移動量を算出するコードなのですがこの場合はMove_Mng()関数部を見てもらいたいのですが**移動速度はの値を使えばかが知りたいす。**moveの値ではおそらく違うと思ます。ようすすにプレイヤーオブジェクト自体移動速度を知りたい?と思うのですがどの方法でどの値を使えばいいのでしょう
2
-
3
-
4
-
5
-
6
-
7
-
1
+ 提示コードFixedUpdate()提示画像デバッグログ何回同じもの出力されたかという数字部分ですが何故か移動中ににかかわ**ログが(0,0,0)で移動中にたまに(2.0,0,0)な**とったおしなログ出力されてしまうのしょうか?**rb.AddForceの後にDebug.Log()を**載せているので直前出力されるはずなのですが原因がわりません。
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+ 質問内容:ログの値がおかしい原因と理由が知りたい。
10
+
11
+ 質問内容2:move.x *= 100などのいった値がmoveに入っていながらも2.0といった値もなぜこんなに小さい値なのかも謎ですこれは正しい可能性もありますが気になったので質問しました。
12
+
13
+ Unityリファレンス: https://docs.unity3d.com/ja/2018.4/ScriptReference/Rigidbody-velocity.html
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+ ![イメージ説明](70e212e1e260323d0655e5dae774dc04.png)
8
26
 
9
27
 
10
28
 
@@ -126,81 +144,81 @@
126
144
 
127
145
  Vector3 forward = transform.forward + slope_range;
128
146
 
129
- Debug.DrawLine(Slope.transform.position, Slope.transform.position + forward ,Color.blue);
147
+ Debug.DrawLine(Slope.transform.position, Slope.transform.position + forward ,Color.blue);
148
+
130
-
149
+ // float speed = Mathf.Sqrt((move.x * move.x) + (move.z * move.z));
150
+
131
-
151
+ float speed = Mathf.Sqrt((rb.velocity.x * rb.velocity.x) + (rb.velocity.z * rb.velocity.z));
152
+
153
+
154
+
132
-
155
+ if(speed != 0){
156
+
133
- if(Physics.Linecast(Slope.transform.position, Slope.transform.position + forward, out hit,LayerMask.GetMask("Slope")) == true)
157
+ if(Physics.Linecast(Slope.transform.position, Slope.transform.position + forward, out hit,LayerMask.GetMask("Slope")) == true)
158
+
159
+ {
160
+
161
+ Debug.Log("坂道のRayヒット!");
162
+
163
+
164
+
165
+ v = new Vector3(0f, (Quaternion.FromToRotation(Vector3.up, hit.normal) * transform.forward * speed).y, 0f) + transform.forward * speed;
166
+
167
+ Debug.Log("v.y: "+ v.y);
168
+
169
+ }
170
+
171
+ }else{
172
+
173
+ v.y = 0;
174
+
175
+ }
176
+
177
+
178
+
179
+ }
180
+
181
+
182
+
183
+
184
+
185
+ void Move_Mng()
186
+
187
+ {
188
+
189
+
190
+
191
+ input_h = Input.GetAxis("Horizontal");
192
+
193
+ input_v = Input.GetAxis("Vertical");
194
+
195
+ Vector3 move_z = new Vector3();
196
+
197
+ Vector3 move_x = new Vector3();
198
+
199
+ move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v;
200
+
201
+ move_x = Camera.main.transform.right * input_h;
202
+
203
+ move = move_x + move_z;
204
+
205
+
206
+
207
+ if (move != Vector3.zero)
134
208
 
135
209
  {
136
210
 
137
- Debug.Log("坂道のRayヒット!");
211
+ transform.rotation = Quaternion.LookRotation(move.normalized);
138
-
139
- ////////////////////////////////////////////////////////////////////////////////////////////////////
140
-
141
- //velocity = new Vector3(0f, (Quaternion.FromToRotation(Vector3.up, stepHit.normal) * transform.forward * walkSpeed).y, 0f) + transform.forward * walkSpeed;
142
-
143
- ///////////////////////////////////////////////////////////////////////////////////////////////////
144
212
 
145
213
  }
146
214
 
147
215
 
148
216
 
149
- }
150
-
151
-
152
-
153
-
154
-
155
- void Move_Mng()
156
-
157
- {
158
-
159
-
160
-
161
- input_h = Input.GetAxis("Horizontal");
162
-
163
- input_v = Input.GetAxis("Vertical");
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
- Vector3 move_z = new Vector3();
172
-
173
- Vector3 move_x = new Vector3();
174
-
175
- move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v ;//* walk_speed;
176
-
177
- move_x = Camera.main.transform.right * input_h; //* walk_speed;
178
-
179
- move = move_x + move_z;
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
- move.x *= 200;
217
+ move.x *= 100;
190
-
218
+
191
- move.z *= 200;
219
+ move.z *= 100;
192
-
193
-
194
-
195
-
196
-
197
- if (move != Vector3.zero)
220
+
198
-
199
- {
221
+
200
-
201
- transform.rotation = Quaternion.LookRotation(move.normalized);
202
-
203
- }
204
222
 
205
223
  }
206
224
 
@@ -222,11 +240,17 @@
222
240
 
223
241
 
224
242
 
243
+
244
+
245
+ // Debug.Log("velocity: " + Mathf.Sqrt((rb.velocity.x * rb.velocity.x) + (rb.velocity.z * rb.velocity.z)));
246
+
247
+
248
+
225
- }
249
+ }
226
-
227
-
228
-
229
-
250
+
251
+
252
+
253
+
230
254
 
231
255
  //////////////////////////////////////////////////////////////////////////////////////////////
232
256
 
@@ -236,15 +260,15 @@
236
260
 
237
261
 
238
262
 
239
- // rb.velocity = new Vector3(move.x, 0, move.z);
240
-
241
263
 
242
264
 
243
265
  rb.AddForce(move.x, 0, move.z);
244
266
 
245
-
246
-
247
- Debug.Log("velocity: " + rb.velocity.magnitude);
267
+ Debug.Log("velocity: " + rb.velocity);
268
+
269
+
270
+
271
+
248
272
 
249
273
  }
250
274