玩家移动触摸输入

时间:2016-04-22 06:40:29

标签: c# unity3d game-engine unityscript

我正在开发一款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;
            }
        }

1 个答案:

答案 0 :(得分:0)

我猜你的代码一直在调用运动部分并且它会弄乱代码,因为你不会检查它是否是touchPhase的开头。

因此,当用户触摸按钮时,会为每个帧调用Move方法,并且由于touchPhase尚未结束(玩家仍在触摸),它会通过此行{{1 }}

这意味着当玩家仍在触摸if (touch.phase != TouchPhase.Ended)时,所有帧都会调用此部分,我认为如果GUItexture已经开始,您应该进行移动:

touchPhase