试图让我的 2D 角色进行二段跳 - 它不起作用,我不知道为什么?

Trying to make my 2D character double jump - it's not working and I don't know why?

我正在制作 2D 平台游戏。到目前为止,这是我的代码。角色只有在接触地面时才会跳跃——但双跳代码不起作用。任何帮助表示赞赏。我是脚本新手,我不明白我做错了什么?

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

public class PlayerController : MonoBehaviour {

    public float speed = 12f, jumpHeight = 30f;
    Rigidbody2D playerBody;
    Transform playerTrans, tagGround;
    bool isGrounded = false;
    public LayerMask playerMask;
    public float maxJumps = 2;
    public float jumpsLeft = 2;



    // Use this for initialization
    void Start ()
    {
        playerBody = this.GetComponent<Rigidbody2D>();
        playerTrans = this.transform;
        tagGround = GameObject.Find(this.name + "/tag_Ground").transform;

    }

    // Update is called once per frame
    public void FixedUpdate ()
    {
        isGrounded = Physics2D.Linecast(playerTrans.position, tagGround.position, playerMask);
        Move();
        Jump();
        DoubleJump();

    }

    private void Move()
    {
        float move = Input.GetAxisRaw("Horizontal") * speed;
        playerBody.velocity = new Vector2(move, playerBody.velocity.y);
    }

    private void Jump()

    {
        if (isGrounded)
        {


            if (Input.GetButtonDown("Jump"))
            {
                playerBody.velocity = new Vector2(playerBody.velocity.x, jumpHeight);


            }


        }

    }
    private void DoubleJump()
    {
        if (Input.GetButtonDown("Jump") && jumpsLeft > 0)
        {
            Jump();
            jumpsLeft--;
        }

        if (isGrounded)
        {
            jumpsLeft = maxJumps;
        }
    }
}

尝试用 DoubleJump 方法的代码替换您的 Jump 方法代码,并在应用跳转之前删除对 IsGrounded 的检查。否则你的角色每次都必须在地上。然后删除 DoubleJump 方法,因为它不再需要了。如果您在游戏后期使用 DoubleJump 作为附加技能,则只需在您的玩家获得该技能时增加 maxJumps。最初将其设置为 1,以便它们每次都必须落地。

        private void Jump() {
        if (isGrounded) {
            jumpsLeft = maxJumps;
        }
        if (Input.GetButtonDown("Jump") && jumpsLeft > 0) {
            playerBody.velocity = new Vector2(playerBody.velocity.x, jumpHeight);
            jumpsLeft--;
        }
    }

你的代码没有多大意义。你应该用一种方法处理你的跳跃并像这样处理它:

private void HandleJump()
{
    if(isGrounded) {
        jumpsLeft = maxJumps;
    }

    if(Input.GetButtonDown("Jump") && jumpsLeft > 0) {
        playerBody.velocity = new Vector2(playerBody.velocity.x, jumpHeight);
        jumpsLeft--;
    }
}

这样你就可以进行三级跳或任意多的跳跃。