提示コードのコメント部「//ジャンプ」なのですがスペースボタンを押しっぱなしにするとピョンピョンとジャンプするのですがその時に数回に一回かランダムの確立で高くジャンプしてしまい結果、ジャンプの挙動が二種類?にランダムに切り替わるようになってしまいますこれはなぜでしょうか?
原因と修正方法を教えてくれますでしょうか?
Player.cs
1public class PlayerController : MonoBehaviour { 2 3 public float JumpForce = 250.0f; 4 public float speed = 0.5f; 5 private bool isJump; 6 private bool isRun; 7 private bool isGround; 8 private Animator animator; 9 [SerializeField] ContactFilter2D filter2d; 10 11 // Use this for initialization 12 void Start () { 13 14 15 animator = GetComponent<Animator>(); 16 isJump = false; 17 isRun = false; 18 // isGround = false; 19 } 20 21 22 // Update is called once per frame 23 void Update () { 24 int key = 0; 25 26 //if(transform.position.y < ) 27 28 //ジャンプ 29 if(Input.GetKey(KeyCode.Space)) 30 { 31 32 if (!isJump) 33 { 34 GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpForce)); 35 animator.SetBool("Jump",true); 36 isJump = true; 37 } 38 } 39 40 if(isJump == false) 41 { 42 animator.SetBool("Jump",false); 43 } 44 45 if (Input.GetKey(KeyCode.RightArrow)) 46 { 47 key = 1; 48 transform.Translate(Vector3.right * speed); 49 50 if(isRun == false && isJump == false) 51 { 52 animator.SetBool("Run",true); 53 54 } 55 } 56 57 58 59 if (Input.GetKey(KeyCode.LeftArrow)) 60 { 61 key = -1; 62 transform.Translate(Vector3.left * speed); 63 64 if(isRun == false && isJump == false) 65 { 66 animator.SetBool("Run",true); 67 } 68 } 69 70 if(key == 0 || isJump == true) 71 { 72 isRun = false; 73 animator.SetBool("Run",false); 74 } 75 76 77 if(key != 0) 78 { 79 transform.localScale = new Vector3(key,1,1); 80 } 81 82 //地面チェック 83 if (GetComponent<Rigidbody2D>().IsTouching(filter2d)) 84 { 85 isJump = false; 86 // isGround = true; 87 } 88 } 89 90 private void OnCollisionEnter2D(Collision2D collision) 91 { 92 isJump = false; 93 // isGround = true; 94 95 } 96}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/03/26 04:46
2019/03/26 05:14