2016-04-22 69 views
-1

我正在开发2D游戏,现在我已经结束了! (幸福)。但我无法让它在android中移动。下面的代码有什么问题?玩家移动触摸输入

当我运行团结控制台应用程序没有指责任何错误。

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; 
      } 
     } 
+3

尝试调试代码使用Unity Remote,您可以在Google Play上找到它。这个应用程序允许您在Unity编辑器中调试触摸。 –

+1

只是次要的东西,如果你有没有想过移植这对PC,你的运动锁定到您的帧速率是一个坏主意! – Draken

+0

@Draken如果为Android =] –

回答

0

我猜你的代码保持调用运动的一部分,它搅乱代码,因为你没有检查它是否是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; 
    } 
}