回答編集履歴

2

問題の箇所を追加した

2022/02/26 13:42

投稿

bboydaisuke
bboydaisuke

スコア5277

test CHANGED
@@ -3,8 +3,12 @@
3
3
  ```csharp
4
4
  playerRigidbody.velocity = transform.rotation * new Vector3(moveX, 0, moveZ) * moveSpeed * Time.deltaTime * 100;
5
5
  ```
6
+
7
+ ```csharp
8
+ playerRigidbody.velocity = Vector3.zero;
9
+ ```
6
10
 
7
- 「垂直方向の動き」つまり落下速度を 0 にしているから、なかなか落ちてこなくなります。
11
+ のように「垂直方向の動き」つまり上昇・落下速度を 0 にしているから、上昇しようとすると止まってしまうし、なかなか落ちてこなくなります。
8
12
  ここで Y 軸方向の速度を 0 にするのではなく、「現在の垂直方向の速度」をそのまま保持するように修正すればよいと思います。
9
13
 
10
14
  あと、コードは結構自己流の感じがします。例えばカメラを動かすには Cinemachine といって Unity にそういう機能があります。その辺は少しずつでもいいので学んでいくと楽になると思います。

1

修正例を追記した

2022/02/26 13:40

投稿

bboydaisuke
bboydaisuke

スコア5277

test CHANGED
@@ -8,3 +8,33 @@
8
8
  ここで Y 軸方向の速度を 0 にするのではなく、「現在の垂直方向の速度」をそのまま保持するように修正すればよいと思います。
9
9
 
10
10
  あと、コードは結構自己流の感じがします。例えばカメラを動かすには Cinemachine といって Unity にそういう機能があります。その辺は少しずつでもいいので学んでいくと楽になると思います。
11
+
12
+ ### 修正例
13
+ 現状からコードをあまり変えない修正であるため、あまり良い書き方とは言えませんが、一例です。
14
+
15
+ ```csharp
16
+ void moveExecution()
17
+ {
18
+ // (中略)
19
+ mouseYpermission = false;
20
+
21
+ // !!ここで velocity の y 要素を 0 にしてしまってるのが問題
22
+ //playerRigidbody.velocity = transform.rotation * new Vector3(moveX, 0, moveZ) * moveSpeed * Time.deltaTime * 100;
23
+ Vector3 velocity = transform.rotation * new Vector3(moveX, 0, moveZ) * moveSpeed * Time.deltaTime * 100;
24
+ velocity.y = playerRigidbody.velocity.y;
25
+ playerRigidbody.velocity = velocity;
26
+
27
+ //マウスの移動に応じて決める
28
+ playerRigidbody.MoveRotation(Quaternion.Euler(0.0f, playerRigidbody.rotation.eulerAngles.y + mouseX * Time.deltaTime * 100.0f, 0.0f));
29
+ // (中略)
30
+ }
31
+ else
32
+ {
33
+ //リセットする
34
+ // !!ここで velocity の y 要素を 0 にしてしまってるのが問題
35
+ // playerRigidbody.velocity = Vector3.zero;
36
+ Vector3 velocity = Vector3.zero;
37
+ velocity.y = playerRigidbody.velocity.y;
38
+ playerRigidbody.velocity = velocity;
39
+ }
40
+ ```