我无法让我的游戏角色执行“双跳”

本文关键字:执行 双跳 游戏角色 我的 | 更新日期: 2023-09-27 18:37:08

我试图让我的角色在 Unity 中使用 C# 进行双跳。这是我正在使用的代码,但它不会让字符双重跳转。有人知道这个问题的解决方案吗?

public class Tito : MonoBehaviour {
public float speed = 1;  //SIDEWAYS SPEED
public float jump = 1;  //JUMP HEIGHT
public float width = 0.5079113f;  //CHARACTER WIDTH
public float score; //TOTAL AMOUNT OF COLLECTED COINS
public GUIText scoreText;
public bool inair = false; // TRUE = TITO IS IN AIR
public bool grounded = false; // TRUE = TITO IS ON THE GROUND
public bool doublejump = false; 
void Start () 
{
    GameObject Text = GameObject.Find("Text");
}

// COIN PICK UP //
void OnTriggerEnter2D(Collider2D other)
{
    if(other.tag == "Coin")
    {
        Debug.Log ("Coins Collected = " + (score));
        (score) += 1;
        Destroy(other.gameObject);
    }
}
void OnCollisionEnter2D(Collision2D coll) 
{
// DEATH HIT //
    if (coll.gameObject.tag == "Dead")
    {
        Debug.Log ("You died! Restarting Game"); 
        Application.LoadLevel (Application.loadedLevel);
    }
// CHECK TO SEE IF GROUNDED //
    if (coll.gameObject.name == "Ground")
    {
        Debug.Log ("Grounded");
        grounded = true;
        inair = false;
        doublejump = false;
    }
}
void Update () 
{
// LOCK ROTATION // 
    {
        transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, 0, 0);
    }

// INPUT - MOVEMENT //
if (Input.GetKey(KeyCode.RightArrow))
    {
        gameObject.transform.Translate(speed, 0, 0);
        transform.localScale = new Vector3 (width, width, width);
    }
if (Input.GetKey(KeyCode.LeftArrow))
    {
        gameObject.transform.Translate(-speed, 0, 0);
        transform.localScale = new Vector3 (-width, width, width);
    }

        if (Input.GetKey(KeyCode.Space))
        {
            if (grounded == true)
            {
                rigidbody2D.velocity = new Vector3 (0, jump, 0);
                Debug.Log ("SingleJump");
                grounded = false;
                doublejump = true;
            }
            else if (doublejump == true)
            {
                rigidbody2D.velocity = new Vector3 (0, jump, 0);
                Debug.Log ("DoubleJump");
                doublejump = false;             
            }
        }
    }
/*
if (grounded == true) // IF IN TOUCH WITH GROUND
    {
        if (Input.GetKey(KeyCode.Space))
        {
        rigidbody2D.velocity = new Vector3 (0, jump, 0);
            Debug.Log ("In-Air");
            inair = true;
            grounded = false;
        }
    }
*/

}

我无法让我的游戏角色执行“双跳”

我能够使用完全不同的方式修复它。它更容易,代码更少,并且像魅力一样工作。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Tito : MonoBehaviour {
    public float jumpspeed = 50;  //JUMP HEIGHT
    public bool grounded = false; // TRUE = TITO IS ON THE GROUND
    public int jumps = 0; // COUNTER TO CHECK THE AMOUNT OF JUMPS
    public int maxJumps = 2; // MAX EXTRA JUMPS
    void OnCollisionEnter2D(Collision2D coll) {
        // CHECK TO SEE IF GROUNDED //
        if (coll.gameObject.name == "Ground") {
            Debug.Log ("Grounded");
            grounded = true;
            jumps = 0;
        }
    }
    void Update() {
        if (Input.GetKeyDown(KeyCode.Space) && jumps < maxJumps) {
            Jump();
        }
    }
    void Jump() {
        rigidbody2D.velocity = new Vector3 (0, jumpspeed, 0);
        jumps = jumps +1;
    }
}

诀窍是检查你什么时候在地面上,什么时候在空中。

if (Input.GetKey(KeyCode.Space))
{
    if(grounded)
    {
        rigidbody2D.velocity.y = 0;
        rigidbody2D.AddForce(new Vector2(0, jumpForce));
        canDoubleJump = true;
    } 
    else if(canDoubleJump)
    {
       canDoubleJump = false;
       rigidbody2D.velocity.y = 0;
       rigidbody2D.AddForce(new Vector2(0, jumpForce));
    }
}

您第一次调用 Input.GetKey(KeyCode.Space)从输入缓冲区中删除了空格键。

您正在从缓冲区读取空格键,但未在方法中使用它。您需要将其更改为:

void Update () 
{
    if (Input.GetKey(KeyCode.Space))
    {
        if (grounded == true)
        {
            rigidbody2D.velocity = new Vector3 (0, jump, 0);
            Debug.Log ("SingleJump");
            grounded = false;
            doublejump = true;
        }
        else if (doublejump == true)
        {
            rigidbody2D.velocity = new Vector3 (0, jump, 0);
            Debug.Log ("DoubleJump");
            doublejump = false;             
       }
    }
}