Skip to content

Instantly share code, notes, and snippets.

@muzudho
Created June 5, 2017 22:16
Show Gist options
  • Save muzudho/e8f9d12c94335b06ee06d07da2286ed4 to your computer and use it in GitHub Desktop.
Save muzudho/e8f9d12c94335b06ee06d07da2286ed4 to your computer and use it in GitHub Desktop.
Unityで2D格闘ゲームの歩行を作るならモーションのExit Time等を設定して足元が滑って見えないようにする ref: http://qiita.com/muzudho1/items/54ab596cb3749ab5b40f
void FixedUpdate()
{
#region 歩行
if (isGrounded)// 接地していれば
{
if (!this.IsJump1Motion)//ジャンプ時の屈伸中ではないなら
{
//左キー: -1、右キー: 1
float leverX = Input.GetAxisRaw(CommonScript.PlayerAndButton_To_ButtonName[playerIndex, (int)ButtonIndex.Horizontal]);
if (leverX != 0)//左か右を入力したら
{
//Debug.Log("lever x = " + x.ToString());
//入力方向へ移動
Rigidbody2D.velocity = new Vector2(leverX * speedX, Rigidbody2D.velocity.y);
//localScale.xを-1にすると画像が反転する
Vector2 temp = transform.localScale;
temp.x = leverX * CommonScript.GRAPHIC_SCALE;
transform.localScale = temp;
if (leverX < 0)
{
if (!anim.GetBool("dashing"))
{
// ダッシュ・アニメーションの開始
anim.SetInteger("leverNeutral", 0);
anim.SetBool("escaping", false);
anim.SetBool("dashing", true);
//if ((int)PlayerIndex.Player1 == playerIndex)
//{
// Debug.Log("Rigidbody2D.velocity.x = " + Rigidbody2D.velocity.x + " ダッシュ!");
//}
anim.SetTrigger("dash");
}
}
else if (0 < leverX)
{
if (!anim.GetBool("escaping"))
{
// エスケープ・アニメーションの開始
anim.SetInteger("leverNeutral", 0);
anim.SetBool("dashing", false);
anim.SetBool("escaping", true);
//if ((int)PlayerIndex.Player1 == playerIndex)
//{
// Debug.Log("Rigidbody2D.velocity.x = " + Rigidbody2D.velocity.x + " エスケープ!");
//}
anim.SetTrigger("escape");
}
}
}
else//左も右も入力していなかったら
{
// 感覚的に、左から右に隙間なく切り替えたと思っていても、
// 入力装置的には、左から右(その逆も)に切り替える瞬間、どちらも押していない瞬間が発生する。
anim.SetInteger("leverNeutral", anim.GetInteger("leverNeutral")+1);
if ( 8 < anim.GetInteger("leverNeutral") )// レバーを放した 数フレーム目から、レバーが離れた判定をすることにする。
{
//横移動の速度を0にしてピタッと止まるようにする
Rigidbody2D.velocity = new Vector2(0, Rigidbody2D.velocity.y);
if ((int)PlayerIndex.Player1 == playerIndex)
{
Debug.Log("Rigidbody2D.velocity.x = " + Rigidbody2D.velocity.x + " ストップ!");
}
anim.SetBool("dashing", false);
anim.SetBool("escaping", false);
}
}
}
}
#endregion
// ジャンプ略
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment