-1

I am working on a 2D game and now I'm at the end of it! (happiness). But I can not make it move in android. What is wrong in the code below?

When I run the application in UNITY console does not accuse any errors.

  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;
            }
        }
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • 3
    Try to debug your code using Unity Remote you can find it on Google Play. This app allows you to debug touches in Unity Editor. – Paweł Marecki Apr 22 '16 at 06:42
  • 1
    Just a minor thing, if you ever think of porting this to PC, locking your movement to your frame rate is a bad idea! – Draken Apr 22 '16 at 06:45
  • @Draken if for android =] – Alan Vieira Rezende Apr 22 '16 at 06:46
  • Read here for more information on why it's generally a bad idea: http://gamedev.stackexchange.com/questions/1589/when-should-i-use-a-fixed-or-variable-time-step You always need to be able to guarantee a constant frame rate at the moment, and that isn't always possible – Draken Apr 22 '16 at 06:55

1 Answers1

0

I guess your code keeps calling the movement part and it messes up the code because you don't check if it is the start of the touchPhase.

So when the user touches the button, the Move method is called for each frame and because touchPhase hasn't ended (player is still touching), it passes this line if (touch.phase != TouchPhase.Ended).

That means that this part is getting called for all the frames while player is still touching the GUItexture and I think that you should just do the movement if the touchPhase has begun:

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;
    }
}
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17