Unity2Dでアクションゲームを作成しています。
壁キックを実装したいのですが良い方法が思いつきません。
コード
C#
1private void Update() 2 { 3 float x = Input.GetAxis("Horizontal"); 4 rb.velocity = new Vector2(x * maxSpeed , rb.velocity.y); //左右キーで移動 5 6 if (Input.GetKeyDown(KeyCode.UpArrow) && isGround) 7 { 8 isJump = true; 9 rb.velocity = new Vector2(rb.velocity.x, jump); //ジャンプ処理 10 } 11 12 if(isJump) 13 { 14 if(Input.GetKeyUp(KeyCode.UpArrow)) 15 { 16 isJump = false; 17 if (rb.velocity.y > 0.5f) 18 { 19 rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y / 2); 20 //ジャンプボタンを離した時にvelosity.yを半分にすることでボタン離しでジャンプキャンセルを実装 21 } 22 } 23 } 24 25 26 if (isGround) //床に触れていたらTrue 27 { 28 animator.SetBool("jump", false); 29 animator.SetBool("fall", false); 30 } 31 else 32 { 33 if(isWall) //壁に触れていたらTrue 34 { 35 if (Input.GetKeyDown(KeyCode.UpArrow)) 36 { 37 //ここに壁ジャンプ処理を書く 38 rb.AddForce(new Vector2(1,1) * 500); 39 } 40 } 41 } 42 43 if(rb.velocity.y > 1.2f) 44 { 45 animator.SetBool("jump", true); //JumpAnimationをOn 46 } 47 else if (rb.velocity.y < -1.2f) 48 { 49 animator.SetBool("fall", true); //落下AnimationをOn 50 if(rb.velocity.y < - 15f) 51 { 52 rb.velocity = new Vector2(rb.velocity.x, -15f);//落下速度の上限固定 53 } 54 } 55 56 animator.SetBool("isGround", isGround); 57 }
試したこと
普通に考えれば斜め方向とかにrigidbody.velocityに斜め上のベクトルを与えたりして飛ばせば良いのですが,
通常の横移動にもvelocityを使っているのでこのままだと壁キックをするときに
rigidbody.velocity = new Vector2(1,1) * power;とかを与えても
コードの上から4行目の
rb.velocity = new Vector2(x * maxSpeed , rb.velocity.y); //左右キーで移動
でX成分の速度が上書きされてしまい、上方向にしか壁ジャンプをしてくれません。
それなら横移動にvelocityではなくAddForceを使えば良いのでは、となるのですがやはりAddForceの動き方は慣性が働いたり初速が遅かったりなどで使いにくいため、やはり横移動にvelocityの採用は外せませんでした。
2Dアクションでよくある壁キックですがどのように工夫すれば実現できるのでしょうか。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。