プレイヤーがジャンプして空中にいるときに左右どちらかのキーを押して離すとなぜか重力が全く効かなくなり無重力っぽい挙動に変わってしますのですがこれは何が原因なのでしょうか?可能性としては左右キー入力の処理のAddForce()のところしか考えられないのですがここは間違えているとは思いえないのでデバックに困っています。教えてくれますでしょうか?
public class PlayerController : MonoBehaviour { public float JumpForce; public float max_speed; public float speedForce; private bool isJump; private bool isRun; private bool isGround; private Animator animator; private Rigidbody2D rb; [SerializeField] ContactFilter2D filter2d; // Use this for initialization void Start () { rb = GetComponent<Rigidbody2D>(); animator = GetComponent<Animator>(); isJump = false; isRun = false; // isGround = false; } void FixedUpdate() { //地面チェック if (GetComponent<Rigidbody2D>().IsTouching(filter2d)) { isJump = false; // isGround = true; } } // Update is called once per frame void Update () { int key = 0; if (Input.GetKey(KeyCode.Space))//スペースキージャンプ入力 { if (!isJump) { //GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpForce)); GetComponent<Rigidbody2D>().AddForce(transform.up * JumpForce); animator.SetBool("Jump", true); isJump = true; } } if (isJump == false)//ジャンプしてないときはジャンプモーションを消す。 { animator.SetBool("Jump",false); } float speedx = Mathf.Abs(rb.velocity.x);//移動速度 if (Input.GetKey(KeyCode.RightArrow))//右キー入力 { key = 1; if (speedx < max_speed) { rb.AddForce( transform.right * key * speedForce); } animator.speed = speedx / 20.0f; if (isRun == false && isJump == false) { animator.SetBool("Run",true); } } //Debug.Log(rb.velocity.x); if (Input.GetKey(KeyCode.LeftArrow))//左キー入力 { key = -1; if (speedx < max_speed) { rb.AddForce( transform.right * key * speedForce); } animator.speed = speedx / 20.0f; if (isRun == false && isJump == false) { animator.SetBool("Run",true); } } //動いていないときまたはジャンプしてる時はRUNモーションを解除 if(key == 0 || isJump == true) { isRun = false; animator.SetBool("Run",false); } if(key != 0)//左右向き修正 { transform.localScale = new Vector3(key,1,1); } //摩擦軽減処理 Vector2 v = new Vector2(0.80f,0); if ((speedx > 0) && key == 0) { Debug.Log("慣性"); if(rb.velocity.x > 0 ) { rb.velocity *= v; } if (rb.velocity.x < 0) { rb.velocity *= v; } } Debug.Log(isJump); } private void OnCollisionEnter2D(Collision2D collision) { isJump = false; // isGround = true; } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/03/26 12:39
退会済みユーザー
2019/03/26 12:52