質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

1069閲覧

Unity 2Dアクションゲーム ジャンプせずに落下した時の落下がゆっくりになってしまう

pokkuru

総合スコア8

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/11/30 06:31

前提・実現したいこと

unity初心者です。2Dアクションゲームを作っているのですが、
ジャンプせずに落下した時に落下がゆっくりになってしまうので、
ジャンプ後に落下した時のような自然な落下にしたいです

発生している問題

ジャンプをしてから落下した時は自然な落下になるが、
ジャンプをせずに落下した時はゆっくりとした落下になってしまう

該当のソースコード

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class player2d : MonoBehaviour 7{ 8 public float speed; 9 private Rigidbody2D rb; 10 public float jumpForce; 11 bool jump = false; 12 Animator animator; 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 rb = GetComponent<Rigidbody2D>(); 18 animator = GetComponent<Animator>(); 19 } 20 21 // Update is called once per frame 22 private void Update() 23 { 24 float bottomY = Camera.main.transform.position.y - Camera.main.orthographicSize * 2; 25 26 if (gameObject.transform.position.y < bottomY) 27 { 28 int sceneIndex = SceneManager.GetActiveScene().buildIndex; 29 SceneManager.LoadScene(sceneIndex); 30 } 31 32 if (Input.GetKeyDown("space") && !jump) 33 { 34 rb.AddForce(Vector2.up * jumpForce); 35 jump = true; 36 } 37 38 39 } 40 41 private void OnCollisionEnter2D(Collision2D other) 42 { 43 if(other.gameObject.CompareTag("Ground")) 44 { 45 jump = false; 46 animator.SetInteger("CharaState", 0); 47 } 48 else 49 { 50 jump = true; 51 } 52 53 } 54 55 56 void FixedUpdate() 57 { 58 float horizontalKey = Input.GetAxis("Horizontal"); 59 60 if(horizontalKey == 0 && jump == false) 61 { 62 rb.velocity = Vector2.zero; 63 } 64 65 if(jump == true) 66 { 67 animator.SetInteger("CharaState",3); 68 } 69 70 if(horizontalKey != 0 && jump == true) 71 { 72 animator.SetInteger("CharaState", 3); 73 } 74 else if(horizontalKey != 0) 75 { 76 77 78 Vector2 liscale = gameObject.transform.localScale; 79 80 if ((liscale.x > 0 && horizontalKey < 0)||(liscale.x < 0 && horizontalKey > 0)) 81 { 82 liscale.x *= -1; 83 gameObject.transform.localScale = liscale; 84 } 85 animator.SetInteger("CharaState", 1); 86 } 87 else if (jump == true) 88 { 89 animator.SetInteger("CharaState", 3); 90 } 91 else 92 { 93 animator.SetInteger("CharaState", 0); 94 } 95 96 Vector2 movement = new Vector2(horizontalKey, 0); 97 rb.AddForce(movement * speed); 98 Debug.Log(GetComponent<Rigidbody2D>().velocity); 99 } 100} 101

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

以下のスクリプトの追加で直りました

c#

1private void OnCollisionExit2D(Collision2D other) 2 { 3 if (other.gameObject.CompareTag("Ground")) 4 { 5 jump = true; 6 } 7 }

回答して頂いた方ありがとうございました

投稿2020/11/30 07:51

pokkuru

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

スクリプトの最後のほうを書き換えてみてください。

C#

1// ... 2// rb.AddForce(movement * speed); 3// ... 4 5// ↑の1行を ↓こう 6rb.velocity = movement + new Vector2(0, rb.velocity.y);

投稿2020/11/30 06:44

編集2020/11/30 06:50
PinoMatcha

総合スコア368

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問