簡體   English   中英

玩家移動觸摸輸入

[英]Player movement touch input

我正在制作 2D 游戲,現在我已經結束了! (幸福)。 但我不能讓它在android中移動。 下面的代碼有什么問題?

當我在 UNITY 控制台中運行應用程序時,不會指責任何錯誤。

  using UnityEngine;
    using System.Collections;

    public class Bee : MonoBehaviour {

        public float velocity;

        public Transform bee;
        private Animator animator;

        public bool isGrounded = true;
        public float force;

        public float jumpTime = 0.4f;
        public float jumpDelay = 0.4f;
        public bool jumped = false;
        public Transform ground;

        private Gerenciador gerenciador;

        // Use this for initialization
        void Start ()
        {
            gerenciador = FindObjectOfType (typeof(Gerenciador)) as Gerenciador;
            animator = bee.GetComponent<Animator> ();
            gerenciador.StartGame ();

        }

        // Update is called once per frame
        void Update ()
        {

            Move();

        }



        void Move()
        {

            isGrounded = Physics2D.Linecast (bee.transform.position, ground.position, 1 << LayerMask.NameToLayer ("Floor"));

            foreach (UnityEngine.Touch touch in Input.touches) {

                if (this.GetComponent<GUITexture> ().HitTest (touch.position)) {

                    if (touch.phase != TouchPhase.Ended) {

                        if (this.name == "Right") {

                            animator.SetBool ("Run", true);
                            bee.transform.Translate (Vector2.right * velocity* Time.deltaTime);
                            bee.transform.eulerAngles = new Vector2 (0, 0);
                        }

                        if (this.name == "Left") {

                            animator.SetBool ("Run", true);
                            bee.transform.Translate (Vector2.right * velocity* Time.deltaTime);
                            bee.transform.eulerAngles = new Vector2 (0, 180);
                        }

                        if (this.name == "Up" && isGrounded && !jumped) {

                            animator.SetTrigger ("JumpB");
                            bee.GetComponent<Rigidbody2D> ().AddForce (transform.up * force);
                            jumpTime = jumpDelay;
                            jumped = true;
                        }
                    }

                    if (touch.phase == TouchPhase.Ended) {
                        animator.SetBool ("Run", false);


                    }
                }
            }


            if (jumpTime >= 0f) {
                jumpTime -= Time.deltaTime;
            }

            if (jumpTime <= 0 && isGrounded && jumped) {

                animator.SetTrigger ("groundB");
                jumped = false;
            }
        }

我猜你的代碼一直在調用運動部分,它弄亂了代碼,因為你沒有檢查它是否是touchPhase的開始。

因此,當用戶觸摸按鈕時,會為每一幀調用Move方法,並且因為touchPhase尚未結束(玩家仍在觸摸),它會通過這條線if (touch.phase != TouchPhase.Ended)

這意味着當玩家仍在觸摸GUItexture時,所有幀都會調用這部分,我認為如果touchPhase已經開始,您應該只進行移動:

if (touch.phase == TouchPhase.Began) {
    if (this.name == "Right") {
        animator.SetBool ("Run", true);
        bee.transform.Translate (Vector2.right * velocity* Time.deltaTime);
        bee.transform.eulerAngles = new Vector2 (0, 0);
    }
    if (this.name == "Left") {
        animator.SetBool ("Run", true);
        bee.transform.Translate (Vector2.right * velocity* Time.deltaTime);
        bee.transform.eulerAngles = new Vector2 (0, 180);
    }
    if (this.name == "Up" && isGrounded && !jumped) {
        animator.SetTrigger ("JumpB");
        bee.GetComponent<Rigidbody2D> ().AddForce (transform.up * force);
        jumpTime = jumpDelay;
        jumped = true;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM