2016-01-29 57 views
0

我在YouTube上关注教程,一切正常,直到现在,除了我只能用这段代码跳转,有什么建议吗?玩家不能双跳

using UnityEngine; 
using UnityEngine.UI; 
using System.Collections; 

public class Player : MonoBehaviour { 
    public float power = 500; 
    public int jumpHeight = 1000; 
    public bool isFalling = false; 
    public int Score; 
    public Text SCORE; 
    public GameObject YOUDIED; 
    private int health = 3; 
    public GameObject health1; 
    public GameObject health2; 
    public GameObject health3; 
    public int Highscore; 
    private bool canDoubleJump; 
    private bool jumpOne; 
    private bool jumpTwo; 

    // Use this for initialization 
    void Start() { 
     YOUDIED.SetActive(false); 
     Highscore = PlayerPrefs.GetInt ("Highscore", 0);  
     jumpOne = false; 
     jumpTwo = false;  
     canDoubleJump = false; 
    } 

    void Update() {  
     if(Input.GetMouseButtonDown(0) && health == 0) { 
      Time.timeScale = 1f; 
      health = 3; 
      Application.LoadLevel(0); 
     } 

     if (health == 3) { 
      health1.SetActive (true); 
      health2.SetActive (true); 
      health3.SetActive (true); 
     } 

     if (health == 2) { 
      health1.SetActive (true); 
      health2.SetActive (true); 
      health3.SetActive (false); 
     } 

     if (health == 1) { 
      health1.SetActive (true); 
      health2.SetActive (false); 
      health3.SetActive (false); 
     } 

     if (health == 0) { 
      health1.SetActive (false); 
      health2.SetActive (false); 
      health3.SetActive (false); 
     } 

     if (health <= 0) { 
      YOUDIED.SetActive (true); 
      Time.timeScale = 0.0f; 
     } 

     SCORE.text = "Score " + Score; 

     transform.Translate(Vector2.right * power * Time.deltaTime); 

     if (Input.GetKeyDown(KeyCode.Space) && isFalling == false) { 
      jumpOne = true; 
      canDoubleJump = true; 
      isFalling = true; 
     } 

     if (Input.GetKeyDown(KeyCode.Space) && isFalling == true && canDoubleJump == true) { 
      jumpTwo = true; 
      canDoubleJump = false; 
     } 

    } 

    void FixedUpdate() { 
     if (jumpOne == true) { 
      GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpHeight); 
      jumpOne = false; 
     } 

     if (jumpTwo == true) { 
      GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpHeight); 
      jumpTwo = false; 
     } 
    } 

    void OnCollisionStay2D(Collision2D coll) { 
     if (coll.gameObject.tag == "Ground") { 
      isFalling = false; 
      canDoubleJump = false; 
    } 

} 

    public void ScorePlus(int NewScore) { 
     Score += NewScore;  

     if(Score >= Highscore) { 
      Highscore = Score; 
      PlayerPrefs.SetInt ("Highscore", Highscore); 
     } 
    } 

    void OnTriggerEnter2D(Collider2D coll) { 
     if (coll.gameObject.tag == "Death") { 
      health -= 1; 
     } 
    } 
} 
+1

你在哪里跳?你需要清楚哪些代码没有按预期工作,以及它现在正在做什么,这不是你想要的。 – krillgar

+0

你可以请张贴你的联系吗? –

+0

我正在投票结束这个题目,因为这个教程中的特殊错误代码没有任何教学价值,因此QA的答案只能与短暂的无关性相关。 – Fattie

回答

0

解决, 我把其他如果陈述,而不是只有一个如果

if (Input.GetKeyDown(KeyCode.Space) && isFalling == false) { 
     jumpOne = true; 
     canDoubleJump = true; 
     isFalling = true; 
    }else if (Input.GetKeyDown(KeyCode.Space) && isFalling == true && canDoubleJump == true) { 
     jumpTwo = true; 
     canDoubleJump = false; 
    } 
0

canDoubleJump应设置为true这里

void OnCollisionStay2D(Collision2D coll) { 
    if (coll.gameObject.tag == "Ground") { 
     isFalling = false; 
     canDoubleJump = false; 
} 

也在第一跳,你需要设置isFalling为true

if (jumpOne == true) { 
     GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpHeight); 
     jumpOne = false; 
     isFalling = true; 
    } 

这应该做的伎俩