unity 2d人物二段跳,多段跳 简单易懂

unity 2d人物二段跳,多段跳 简单易懂

开门见山,代码见英雄

续前面一篇2d游戏人物跳跃移动unity 2d游戏跳跃移动 手把手教你


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Jump : MonoBehaviour
{
    public Transform jiao;
    public LayerMask ground;
    public float jumpSpeed = 2f;

    public bool isGround;

    public int jumpCount;

    private Rigidbody2D playRig;
    
    //这里判断是否进行第一跳,只有进行了第一跳才能进行第二跳,
    //这里防止初始玩家在空中坠落玩家按下跳跃键进入二段跳逻辑
    private bool one = false;
    
    // Start is called before the first frame update
    void Start()
    {
        playRig = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        isGround = Physics2D.OverlapCircle(jiao.position, 0.1f, ground);

        if (Input.GetButtonDown("Jump"))
        {
            if (jumpCount > 0 && isGround)
            {
                playRig.velocity = Vector2.up * jumpSpeed;
                jumpCount=0;
                one = true;
                Debug.Log("第一段"+isGround);
            }
            else if (jumpCount == 0&&one)
            {
                Debug.Log("第二段"+isGround);
                playRig.velocity = Vector2.up * jumpSpeed;
                isGround = false;
                jumpCount--;
            }
        }
    }

    //这里碰撞到ground才能赋值jumpCount,不能在update里面的isGround下面进行判定,
    //因为他是一个球形范围判定,当玩家跳跃后有段距离还存在isGround判定范围内,会影响下面的JumpCound的赋值
    private void OnCollisionEnter2D(Collision2D col)
    {
        jumpCount = 1;
        one = false;
    }
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值