我正试图通过C#让我的角色在Unity中双跳。这是我正在使用的代码,但它不会让角色双跳。任何人都知道这个问题的解决方案吗?
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;
}
}
*/
}
答案 0 :(得分:1)
我能够以完全不同的方式修复它。它更容易,更少的代码和魅力的工作。
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;
}
}
答案 1 :(得分:0)
首先调用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;
}
}
}
答案 2 :(得分:0)
诀窍是检查你何时在地上以及何时在空中。
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));
}
}