土狼时间(Coyote Time):土狼时间指游戏角色在离开平台(!isGround)后仍被允许跳跃的一段时间,该名源自《Looney Tunes》中歪心狼的经典画面
实现方法:
1.正常跳跃
void Jump()
{
if (isGround)
{
jumpCount = MaxjumpCount;
isJump = false;
}
if (jumpPressed)
{
isJump = true;
rb.velocity = new Vector2(rb.velocity.x,jumpForce);
jumpCount--;
jumpPressed = false;
}
else if(jumpPressed && isJump)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce*0.8f);
jumpCount--;
jumpPressed = false;
}
这里做的是一个二段跳跳跃,如果正常的二段跳跳跃,条约判定应该这样写:
if (jumpPressed&&isGround)
{
isJump = true;
rb.velocity = new Vector2(rb.velocity.x,jumpForce);
jumpCount--;
jumpPressed = false;
}
jumpPressed的判定以及地面检测器如下:
public Transform groundCheck;
public LayerMask ground;
void Update()
{
isGround = Physics2D.OverlapCircle(groundCheck.position, 0.1f, ground);
if (Input.GetButtonDown("Jump") && jumpCount > 0)
{
if(isGround||CoyoteTime > 0||isJump)
jumpPressed = true;
}
}
2.土狼时间:
public float CoyoteTime,SetCoyoteTime;
void CoyoteTimeJump()
{
if(isGround || CoyoteTime > 0||isJump)//在地面或土狼时间中或二段跳中可起跳
{
Jump();
}
if (rb.velocity.y > 0.01f && !isGround)//角色已起跳没有土狼时间
{
CoyoteTime = 0;
}
if (isGround)
CoyoteTime = SetCoyoteTime;//落地土狼时间重置
else
CoyoteTime -= Time.deltaTime;//滞空土狼时间计时
}