【发布时间】:2020-04-08 07:02:20
【问题描述】:
我对这一切都很陌生,对于新手的错误,我深表歉意。我正在制作一个 2D 平台游戏 我正在通过 RayCast 检查地面,在玩家跳跃后,jumpCounter 重置为 0,所以我得到了奖励跳跃。我尝试在重置之前添加一个 0.2 秒的计时器,但这会在连续多次跳跃(兔子跳跃)时造成麻烦。有什么帮助吗?
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;
}
}
【问题讨论】:
-
HurtCheck在做什么? -
我感觉
Vector2.down * hitDistance的值太大了,并且射线在跳转后的第一帧到达平台,因此将其重置为0。尝试在脚本中使用OnCollisionEnter地板进行重置。 -
这解决了我的问题。 Raycast 太大了,但我需要它来在斜坡上运行动画,所以我创建了一个不同的并将
IsGrounded移动到OnCollisionEnter2D。非常感谢您的帮助!