unity2D//终极跳跃(可实现二段跳)

该博客介绍了Unity 2D游戏开发中角色移动和跳跃的实现,包括使用Rigidbody2D、Collider2D和Animator组件来处理角色的横向移动、跳跃以及动画状态切换。代码详细展示了如何响应用户输入,检测地面以实现跳跃,并通过FixedUpdate和Update方法更新角色状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

public class FinalMove : MonoBehaviour
{
    private Rigidbody2D rig;
    private Collider2D coll;
    private Animator anim;
  //  public Collider2D discoll;//用于下蹲关闭头部碰撞体

    public float speed, jumpforce;

    public Transform groundcheck;//检测地面
  //  public Transform CellingCheck;//检测顶部
    public LayerMask ground;

    public bool isGround, isJump;

    bool jumpPressed;//检验跳跃按键是否按下
    int jumpCount;//跳跃次数

    void Start()
    {
        rig = GetComponent<Rigidbody2D>();
        coll = GetComponent<Collider2D>();
        anim = GetComponent<Animator>();
    }
 
    void Update()
    {
        if(Input.GetButtonDown("Jump")&&jumpCount>0)
        {
            jumpPressed = true;
        }
    } 
    private void FixedUpdate()
    {
        GroundMove();
        Jump();
    
        isGround = Physics2D.OverlapCircle(groundcheck.position, 0.1f, ground);
        SwitchAnim();
    }
    void GroundMove()
    {
        //左右移动
        float horizontalmove= Input.GetAxisRaw("Horizontal");//-1、0、1
        rig.velocity = new Vector2(horizontalmove * speed, rig.velocity.y);
        //面朝方向
        if(horizontalmove != 0)
        {
            transform.localScale = new Vector3(horizontalmove, 1, 1);
        }
     
    }
    void Jump()//跳跃
    {
        if(isGround)
        {
            jumpCount = 2;
            isJump = false;
        }
        if (jumpPressed && isGround)
        {
            isJump = true;
            rig.velocity = new Vector2(rig.velocity.x, jumpforce);
            jumpCount--;
            jumpPressed = false;
        }
        else if (jumpPressed && jumpCount > 0&&!isGround)
        {
            isJump = true;
            rig.velocity = new Vector2(rig.velocity.x, jumpforce);
            jumpCount--;
            jumpPressed = false;
        }
    }

    void SwitchAnim()//角色动画
    {
        anim.SetFloat("running", Mathf.Abs(rig.velocity.x));
        if(isGround)
        {
            anim.SetBool("falling", false);
        }
        else if(!isGround&&rig.velocity.y>0)
        {
            anim.SetBool("jumping", true);
        }
        else if(rig.velocity.y<0)
        {
            anim.SetBool("jumping", false);
            anim.SetBool("falling", true);
            if(coll.IsTouchingLayers(ground))
            {
                anim.SetBool("falling", false);
                anim.SetBool("idle", true);
            }
        }

    }
    /*void Crouch()//角色下蹲
    {
        if (!Physics2D.OverlapCircle(CellingCheck.position, 0.2f, ground))//判断蹲下后是否站起(没有障碍物时才执行)
        {
            if (Input.GetButton("Crouch"))
            {
                anim.SetBool("crouching", true);
                discoll.enabled = false;//关闭 Box collider
            }
            else
            {
                anim.SetBool("crouching", false);
                discoll.enabled = true;//打开 Box collider
            }
        }
    }*/
}

一个字:绝!
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值