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;
}
}