游戏角色不会双跳c#

时间:2015-12-02 18:24:44

标签: c# 2d platform

我正在努力创建一个统一的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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题是您正在检查跳转按钮当前是否处于关闭状态。按下并释放按钮通常发生在多个帧上(即Update()被称为许多帧按下按钮的时间。)

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

最简单的(也可能是最好的)是进行此更改:

if(Input.GetButtonDown("Jump"))

GetButtonDown仅对第一次按下按钮的帧返回true,为false,直到释放并再次按下该按钮为止。

另一个是包含第二个变量,该变量在释放按钮之前阻止激活第二个块。这不太理想,但会显示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;
        }
    }

请注意,这并不能解决双跳能力通常包含的“跌落平台并跳跃一次”。我将把它作为练习留给读者。

相关问题