游戏角色不会双跳c#

Game character won't double jump c#

我正在尝试在 unity 中创建一个 2D 平台游戏,当我尝试让角色进行二段跳时,它不起作用。我想知道我是否可以得到任何帮助。

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public float maxSpeed = 3;
    public float speed = 50f;
    public float jumpPower = 150f;

    public bool grounded;
    public bool canDoubleJump;

    private Rigidbody2D rb2d;
    private Animator anim;

    void Start ()
    {
        rb2d = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<Animator>();
    }


    void Update ()
    {

        anim.SetBool("Grounded", grounded);
        anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));

        if(Input.GetAxis("Horizontal") < -0.1f)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }

        if(Input.GetAxis("Horizontal") > 0.1f)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }

        if(Input.GetButton("Jump"))
        {
            if(grounded)
            {
                rb2d.AddForce(Vector2.up * jumpPower);
                canDoubleJump = true;
            }
            else
            {
                if (canDoubleJump)
                {
                    canDoubleJump = false;
                    rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
                    rb2d.AddForce(Vector2.up * jumpPower);

                }
            }
        }

    }

    void FixedUpdate()
    {
        Vector3 easeVelocity = rb2d.velocity;
        easeVelocity.y = rb2d.velocity.y;
        easeVelocity.z = 0.0f;
        easeVelocity.x *= 0.75f;

        float h = Input.GetAxis("Horizontal");

        //fake friction / easing x speed
        if(grounded)
        {
            rb2d.velocity = easeVelocity;
        }


        //moving player
        rb2d.AddForce((Vector2.right * speed) * h);

        //limiting speed
        if(rb2d.velocity.x > maxSpeed)
        {
            rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
        }

        if(rb2d.velocity.x < -maxSpeed)
        {
            rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
        }
    }
}

问题是您正在检查跳转按钮 当前是否处于按下状态。 按下和释放按钮通常发生在多个帧上(即 Update()在按下按钮的过程中被多次调用)。

有两种方法可以解决此问题。

最简单(也可能是最好)的方法是进行以下更改:

if(Input.GetButtonDown("Jump"))

GetButtonDown 仅 returns 第一次按下按钮的帧为真,释放并再次按下之前为假。

另一个是包含第二个变量,以防止在释放按钮之前激活第二个块。这不太理想,但显示了 GetButtonDown.

幕后发生的事情
var isButtonDown = false;

Update() {
        if(Input.GetButton("Jump"))
        {
            if(grounded)
            {
                rb2d.AddForce(Vector2.up * jumpPower);
                canDoubleJump = true;
                isButtonDown = true;
            }
            else if(!isButtonDown)
            {
                if (canDoubleJump)
                {
                    canDoubleJump = false;
                    rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
                    rb2d.AddForce(Vector2.up * jumpPower);

                }
            }
        }
        else {
            isButtonDown = false;
        }
    }

请注意,这并未解决 "fall off a platform and jump once" 双跳能力通常包含的问题。我将把它作为练习留给 reader.