【问题标题】:Jump in Unity 2D跳转到 Unity 2D
【发布时间】:2021-09-13 20:33:39
【问题描述】:

我在跳转 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;
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多