2016-01-29 87 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; 
    } 

這應該做的伎倆