对于所有这些我都是新手,所以对菜鸟的错误感到抱歉。我正在制作2D平台游戏 我正在通过RayCast检查地面,并在玩家跳跃后立即将jumpCounter重置为0,因此我获得了奖励跳跃。我尝试添加一个0.2s的计时器,然后它才能重置,但是连续跳多次(兔子跳)时会造成麻烦。有帮助吗?
private void FixedUpdate()
{
GroundCheck();
if (state != State.hurt)
{
Movement();
DoubleJump();
StateMachine();
}
HurtCheck();
anim.SetInteger("state", (int)state);
}
private void Movement()
{
//Input.GetAxis returns a value between -1 up to 1
//Edit> Project Settings> Input> Axis> Horizontal
float hDirection = Input.GetAxisRaw("Horizontal");
//holding down "D" makes the value positive and vice versa
if (hDirection < 0)
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
transform.localScale = new Vector2(-1, 1);
}
else if (hDirection > 0)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
transform.localScale = new Vector2(1, 1);
}
else
{
}
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
Jump();
}
}
private void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, jumpforce);
state = State.jumping;
jumpCount += 1;
}
private void GroundCheck()
{
Debug.DrawRay(transform.position, Vector2.down * hitDistance, Color.green);
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, hitDistance, ground);
if(hit.collider != null)
{
isGrounded = true;
jumpCount = 0;
}
else
{
isGrounded = false;
}
}
private void DoubleJump()
{
if (Input.GetButtonDown("Jump") && isGrounded == false && jumpCount < 2)
{
rb.velocity = new Vector2(rb.velocity.x, jumpforce);
jumpCount += 1;
}
}
答案 0 :(得分:0)
嘿,您是否确定播放器的图层不是ground
图层或没有正确设置ground
图层蒙版,或者hitDistance
的{{1}}太高了,应该非常低,例如raycast
或更小,或者0.1
从jumpCount
或0
开始尝试交替进行,或者让它从两个开始失去额外的奖金跳跃...这些就是我要看的地方,祝你好运!