2016-09-09 7 views
0

Ich habe ein Problem, wenn ich GameObject sprang. Ich habe einen Spieler und einen Boden gemacht. Der Spieler läuft auf dem Boden und ich kurze Berührung, Spieler wird normal springen und lange berühren, Spieler wird Hochsprung. Aber manchmal springt der Spieler nicht, wenn der Spieler Boden berührt. Ich dachte, dieses Problem bezieht sich auf die Schwerkraft oder Masse des Spielers. Dieses Spiel wurde auf der Unity 2d Platform erstellt. Sehen Sie diesen Code (Player-Code):Sprung in Unity 2D

using UnityEngine; 
using System.Collections; 
using UnityEngine.Events; 
using UnityEngine.EventSystems; 
using UnityEngine.UI; 

public class HeroRun : MonoBehaviour{ 


    public static HeroRun instance; 

    //public float force; 
    //private bool isRacePressed = false; 
    public bool isTouchEnemy = false; 
    public Rigidbody2D myBody; 
    public bool touchGround = false; 
    //private float mouseTime = 0; 

    private float timeLongTouch = 0.2f; 
    private float timeTouchBegan; 
    public float jumpForce; 
    public float highJumpForce; 


    int coin = 0; 

    void Awake(){ 
     myBody = GetComponent<Rigidbody2D>(); 



    } 
    // Use this for initialization 
    void Start() { 
     MakeSingleInstance(); 
    } 

    // Update is called once per frame 
    void FixedUpdate() { 
     #if UNITY_EDITOR 
     Editor(); 
     #elif UNITY_IPHONE 
     Iphone(); 
     #endif 
    } 
    void Editor(){ 
     if(Input.GetMouseButtonDown(0) && touchGround){ 
      myBody.velocity = 
       new Vector2 (myBody.velocity.x, highJumpForce*Time.deltaTime); 
      touchGround = false; 
     } 
    } 
    void Iphone(){ 
     if (touchGround && !isTouchEnemy) { 
      //Debug.Log (Time.time); 
      if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began) { 
       timeTouchBegan = Time.time; 
      } 
      if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary) { 
       if (Time.time - timeTouchBegan >= timeLongTouch) { 
        myBody.velocity = 
         new Vector2 (myBody.velocity.x, highJumpForce*Time.deltaTime); 
        touchGround = false; 
       } 
      } 
      if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended) { 
       if (Time.time - timeTouchBegan < timeLongTouch) { 
        myBody.velocity = 
         new Vector2 (myBody.velocity.x, jumpForce * Time.deltaTime); 
        touchGround = false; 
       } 
      } 
     } 
    } 
    void MakeSingleInstance(){ 
     if (instance == null) { 
      instance = this; 
     } 
    } 
    void OnCollisionEnter2D (Collision2D target){ 
     if (target.gameObject.tag == "Ground") { 
      touchGround = true; 
     } 
    } 
    void OnCollisionExit2D(Collision2D target){ 
     if (target.gameObject.tag == "Ground") { 
      touchGround = false; 
     } 
    } 
    void OnCollisionStay2D(Collision2D target){ 
     if (target.gameObject.tag == "Ground") { 
      touchGround = true; 
     } 
    } 
// void OnTriggerExit2D(Collider2D target){ 
//  if (target.tag == "Ground") { 
//   touchGround = false; 
//  } 
// } 
    void OnTriggerEnter2D(Collider2D target){ 
//  if (target.tag == "Ground") { 
//   touchGround = true; 
//  } 
     if (target.tag == "Coins") { 
      coin++; 
      GameController.instance.coinText.text = "" + coin; 
      Destroy (target.gameObject); 
     } 
     if (target.tag == "SlowEnemy") { 

      isTouchEnemy = true; 
      StartCoroutine ("SlowSpeedForSlowEnemy"); 
     } 
     if (target.tag == "JumpEnemy") { 
      Debug.Log ("Em chỉ vào đây 1 lần"); 
      //myBody.AddForce (new Vector2(0, 900f)); 
      myBody.velocity = new Vector2(myBody.velocity.x, 900f * Time.deltaTime); 
      isTouchEnemy = true; 
      //StartCoroutine (NormalRun (.25f)); 
     } 
     if (target.tag == "FallEnemy") { 
      GroundController.instance.speed = 5f; 
      BGController.instance.speeds = 2f; 
      isTouchEnemy = true; 
      StartCoroutine (NormalRun (.2f)); 
     } 
    } 
    IEnumerator SlowSpeedForSlowEnemy(){ 
     yield return new WaitForSeconds (0.1f); 
     BGController.instance.speeds = 2/5f; 
     GroundController.instance.speed = 1f; 
     StartCoroutine ("DisableIsTouchEnemy"); 
     StartCoroutine ("GrowSpeedForSlowEnemy"); 
    } 
    IEnumerator DisableIsTouchEnemy(){ 
     yield return new WaitForSeconds (.09f); 
     isTouchEnemy = false; 
    } 
    IEnumerator GrowSpeedForSlowEnemy(){ 
     yield return new WaitForSeconds (0.05f); 
     if (BGController.instance.speeds < 3f) { 
      BGController.instance.speeds += 0.1f; 
     } 
     if (GroundController.instance.speed < 7f) { 
      GroundController.instance.speed += 0.5f; 
     } 
     if (BGController.instance.speeds >= 3f && GroundController.instance.speed >= 7f) { 
      yield break; 
     } 
     StartCoroutine ("GrowSpeedForSlowEnemy"); 
    } 
    IEnumerator NormalRun(float seconds){ 
     yield return new WaitForSeconds (seconds); 
     GroundController.instance.speed = 7f; 
     BGController.instance.speeds = 3f; 
     isTouchEnemy = false; 
    } 
} 

Antwort

Verwandte Themen