Unity2D 实现土狼时间跳跃

土狼时间(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;//滞空土狼时间计时

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值