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

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

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

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

Unity

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

Q&A

解決済

1回答

1261閲覧

ジャンプの高さがランダムになってしまう

pokkuru

総合スコア8

C#

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

Unity

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

0グッド

0クリップ

投稿2021/05/25 05:49

編集2021/05/25 06:05

前提・実現したいこと

spaceキーを押す長さでジャンプの高さを変えようとしています。
しかし、spaceキーを長く押した時でも、
高くジャンプする時と、低くジャンプするする時があったりして、
ジャンプの高さがランダムになってしまいます。

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class player2d : MonoBehaviour 7{ 8 public float speed; 9 public float normalSpeed = 10; 10 public float shiftSpeed = 15; 11 public float normalJump = 450; 12 public float shiftJump = 540; 13 private bool jumpUpEnd = false; 14 [SerializeField]public float jumpTime; 15 private Rigidbody2D rb; 16 public float jumpForce; 17 public float jumpHeight; 18 public float jumpLimitTime; 19 bool jump = false; 20 Animator animator; 21 private ContactFilter2D contact; 22 23 24 // Start is called before the first frame update 25 void Start() 26 { 27 rb = GetComponent<Rigidbody2D>(); 28 animator = GetComponent<Animator>(); 29 contact = GetComponent<ContactFilter2D>(); 30 } 31 32 // Update is called once per frame 33 private void Update() 34 { 35 bool isTouch = rb.IsTouching(contact); 36 if (gameObject.transform.position.y < -20) 37 { 38 int sceneIndex = SceneManager.GetActiveScene().buildIndex; 39 SceneManager.LoadScene(sceneIndex); 40 } 41 42 if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) 43 { 44 speed = shiftSpeed; 45 jumpForce = shiftJump; 46 } 47 else 48 { 49 speed = normalSpeed; 50 jumpForce = normalJump; 51 } 52 53 if(isTouch) 54 { 55 jumpTime = 0.0f; 56 } 57 58 59 if (Input.GetButtonDown("Jump") && !jumpUpEnd && jumpTime < 0.3f) 60 { 61 62 jumpTime += Time.deltaTime; 63 rb.velocity = new Vector2(rb.velocity.x, jumpForce * Time.deltaTime); 64 jump = true; 65 } 66 67 if (Input.GetButtonUp("Jump") && !jumpUpEnd) 68 { 69 //二回ジャンプできなくする 70 jumpUpEnd = true; 71 } 72 } 73 74 private void OnCollisionEnter2D(Collision2D other) 75 { 76 if (other.gameObject.CompareTag("Ground")) 77 { 78 jumpUpEnd = false; 79 jump = false; 80 animator.SetInteger("CharaState", 0); 81 } 82 83 if (other.gameObject.CompareTag("moveground")) 84 { 85 jumpUpEnd = false; 86 jump = false; 87 animator.SetInteger("CharaState", 0); 88 } 89 90 if (other.gameObject.tag == "moveground") 91 { 92 transform.SetParent(other.transform); 93 } 94 95 } 96 97 98 private void OnCollisionExit2D(Collision2D other) 99 { 100 if (other.gameObject.CompareTag("Ground")) 101 { 102 jump = true; 103 } 104 105 if (other.gameObject.CompareTag("moveground")) 106 { 107 jump = true; 108 } 109 110 if (other.gameObject.tag == "moveground") 111 { 112 transform.SetParent(null); 113 } 114 } 115 116 void FixedUpdate() 117 { 118 float horizontalKey = Input.GetAxis("Horizontal"); 119 120 if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) 121 { 122 speed = shiftSpeed; 123 jumpForce = shiftJump; 124 } 125 else 126 { 127 speed = normalSpeed; 128 jumpForce = normalJump; 129 } 130 131 if ((horizontalKey <= 0.1 && horizontalKey >= -0.1 && jump == false) || Input.GetKeyUp(KeyCode.DownArrow) && jump == false || Input.GetKeyUp(KeyCode.UpArrow) && jump == false || Input.GetKeyUp(KeyCode.RightArrow) && jump == false || Input.GetKeyUp(KeyCode.LeftArrow) && jump == false) 132 { 133 rb.velocity = Vector2.zero; 134 } 135 136 if(Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.LeftArrow) && jump == false) 137 { 138 rb.velocity = Vector2.zero; 139 } 140 141 if (jump == true) 142 { 143 animator.SetInteger("CharaState", 3); 144 } 145 146 if (horizontalKey != 0 && jump == true) 147 { 148 animator.SetInteger("CharaState", 3); 149 } 150 else if (horizontalKey != 0) 151 { 152 153 154 Vector2 liscale = gameObject.transform.localScale; 155 156 if ((liscale.x > 0 && horizontalKey < 0) || (liscale.x < 0 && horizontalKey > 0)) 157 { 158 liscale.x *= -1; 159 gameObject.transform.localScale = liscale; 160 } 161 animator.SetInteger("CharaState", 1); 162 } 163 else if (jump == true) 164 { 165 animator.SetInteger("CharaState", 3); 166 } 167 else 168 { 169 animator.SetInteger("CharaState", 0); 170 } 171 172 rb.velocity = new Vector2(horizontalKey * speed, rb.velocity.y); 173 174 if(jump == true) 175 { 176 rb.velocity = new Vector2(horizontalKey * speed, rb.velocity.y); 177 178 } 179 if(rb.velocity.x > 6) 180 { 181 rb.velocity = new Vector2(6, rb.velocity.y); 182 } 183 184 if (rb.velocity.x < -6) 185 { 186 rb.velocity = new Vector2(-6, rb.velocity.y); 187 } 188 189 //Debug.Log(horizontalKey); 190 } 191 192 193} 194

試したこと

インスペクターでjumpTimeを見たところ、
ジャンプをした時に作動する時としない時がありました。

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

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

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

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

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

m.ts10806

2021/05/25 05:58

C は本件とは無関係と思います
pokkuru

2021/05/25 06:06

すいません。直しておきました
Y0241-N

2021/05/25 07:08

Update内とFixedUpdate内で if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { speed = shiftSpeed; jumpForce = shiftJump; } else { speed = normalSpeed; jumpForce = normalJump; } が重複しているのでまずこれはどちらか一方にしましょう、入力値を取る場合はUpdate内で記述した方が良いのでFixedUpdate内の記述を消した方が良いです。 また、「spaceキーを押す長さでジャンプの高さを変えようとしています」とありますが、 見たところjumpForceが変更されるタイミングがShiftKeyが押されているかいないかの時しか変化がないので、これでは長押し云々でジャンプ力が変わることはないように思います。
guest

回答1

0

ベストアンサー

GetButtonDown関数はボタンを押した瞬間のプレームの間だけtrueを返します。
1フレームは人間の感覚ではものすごい短い間に過ぎてしまいます。
そのため、その間で押している時間を加算しても欲しい値にならないのでしょう。
ボタンを押し続けていることを確認するには GetButton関数を用います。
これがどのくらいの間trueになっているかを見てジャンプの大きさを決めるというのがこの問題を解決する簡単な方法かと思います。

投稿2021/05/29 02:55

monmoko

総合スコア202

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問