2016-09-09 7 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; 
    } 
} 

ответ

0

1) Добавление скорости к объекту, чтобы сделать его прыжок плохая практика. Вместо этого используйте функцию AddForce.

2) Используйте Linecast для обнаружения касания земли. Linecasts более эффективны, чем использование функций обратного вызова коллайдера. Проверьте эту статью http://answers.unity3d.com/questions/610960/2d-check-if-player-is-on-the-ground.html

Смежные вопросы