前提・実現したいこと
Unityで2Dゲームを作るにあたり、キャラクターが接地している場合のみジャンプするようにしたいのですが、空中でもジャンプしてしまいます。修正点がわからず、質問させていただきました。接地判定のコードも載せさせていただきます。
始めたばかりで見難いコードとは思いますが宜しくお願いいたします。
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerController : MonoBehaviour 6{ 7 Rigidbody2D rigid2D; 8 float jumpforce = 600.0f; 9 10 public GroundCheck ground; 11 private bool isGround = false; 12 13 void Start() 14 { 15 this.rigid2D = GetComponent<Rigidbody2D>(); 16 } 17 void Update() 18 { 19 isGround = ground.IsGround();//接地判定スクリプトのIsGroundメソッドの呼び出し 20 21 if (Input.GetKeyDown(KeyCode.Space) && isGround) 22 { 23 this.rigid2D.AddForce(transform.up * this.jumpforce); 24 isGround = false; 25 } 26 } 27} 28 29以下は接地判定のスクリプトです。 30 31---省略--- 32 private string GroundTag = "Ground"; 33 private bool isGround = false; 34 private bool isGroundEnter, isGroundStay, isGroundExit; 35 36 public bool IsGround() 37 { 38 if(isGroundEnter || isGroundStay) 39 { 40 isGround = true; 41 }else if(isGroundExit) 42 { 43 isGround = false; 44 } 45 isGroundEnter = false; 46 isGroundStay = false; 47 isGroundExit = false; 48 return isGround; 49 } 50 51 private void OnTriggerEnter2D(Collider2D collision) 52 { 53 if (collision.tag == GroundTag). 54 { 55 isGroundEnter = true; 56 } 57 58 } 59 private void OnTriggerStay2D(Collider2D collision) 60 { 61 if(collision.tag == GroundTag) 62 { 63 isGroundStay = true; 64 } 65 66 } 67 private void OnTriggerExit2D(Collider2D collision) 68 { 69 if(collision.tag == GroundTag) 70 { 71 isGroundExit = true; 72 73 } 74 75 } 76 77} 78 79
試したこと
補足情報(FW/ツールのバージョンなど)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/15 15:43 編集
2020/02/16 01:57
2020/02/16 05:28