2016-09-09 4 views
0

GameObject를 뛰어 넘을 때 문제가 발생했습니다. 나는 선수와 땅을 만들었다. 플레이어가 땅에서 뛰고 짧게 터치하면 플레이어가 정상적으로 점프하고 길게 터치하며 플레이어는 높은 점프를합니다. 하지만 플레이어가 그라운드에 닿았을 때 플레이어가 뛰지 않는 경우가 있습니다. 나는이 문제가 중력 또는 플레이어의 질량과 관련이 있다고 생각했다. 이 게임은 Unity 2D 플랫폼에서 제작되었습니다. 이 코드를 참조하십시오 (플레이어 코드) :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; 
    } 
} 

답변