跳入Unity 2D

时间:2016-09-09 05:02:13

标签: unityscript

当我跳过GameObject时,我遇到了问题。我做了一个球员和地面。玩家在地面上跑,我短暂的触摸,玩家将正常跳跃和长时间触摸,玩家将跳高。但是当玩家接触地面时,玩家有时不会跳跃。我认为这个问题与玩家的重力或质量有关。这个游戏是在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;
    }
}

1 个答案:

答案 0 :(得分:0)

1)向对象添加速度以使其跳跃是一种不好的做法。请改用AddForce函数。

2)使用Linecasts检测地面触摸。 Linecast比使用Collider回调函数更有效。查看此文章http://answers.unity3d.com/questions/610960/2d-check-if-player-is-on-the-ground.html