【问题标题】:Player movement touch input玩家移动触控输入
【发布时间】:2016-08-15 15:54:41
【问题描述】:

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

【问题讨论】:

  • 尝试使用 Unity Remote 调试您的代码,您可以在 Google Play 上找到它。这个应用程序允许您在 Unity 编辑器中调试触摸。
  • 只是一件小事,如果你想把它移植到 PC 上,将你的动作锁定到你的帧速率是个坏主意!
  • @Draken if for android =]
  • 阅读此处了解为什么它通常是一个坏主意的更多信息:gamedev.stackexchange.com/questions/1589/… 您现在总是需要能够保证恒定的帧速率,但这并不总是可能的

标签: c# unity3d game-engine unityscript


【解决方案1】:

我猜你的代码一直在调用移动部分,它弄乱了代码,因为你没有检查它是否是 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;
    }
}

【讨论】:

    猜你喜欢
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多