触摸和拖动问题Unity 2D游戏

时间:2016-04-16 15:42:17

标签: android touch 2d unity5

我的触摸控件存在一些问题。

1)我的游戏是2D。当我在我的Android设备上测试它时,当从右触摸移动到左触摸时,2D图像几乎消失。该对象被视为3D对象。我认为这与Z空间有关。

2)如何让角色像这个视频一样移动 - https://www.youtube.com/watch?v=5hckY75j_lg。在这种情况下,您触摸屏幕的任何地方都会抓住玩家(头部),并且与手指完全一样移动。

任何帮助都会受到赞赏!

这是我的代码:

公共类TouchLogic:MonoBehaviour {

 public static int currTouch = 0;
 private Ray ray;
 private RaycastHit rayHitInfo = new RaycastHit ();
 [HideInInspector ]
 public int touch2Watch=64;

 // Update is called once per frame
 void Update () {
     if (Input.touches.Length <= 0) {
     } else {
         for (int i = 0; i < Input.touchCount; i++) {
             currTouch = i;
             if (this.GetComponent <GUITexture >() != null && (this.GetComponent <GUITexture >().HitTest (Input.GetTouch (i).position))) {
                 if (Input.GetTouch (i).phase == TouchPhase.Began) {
                     this.SendMessage ("OnTouchBegan");
                 }
                 if (Input.GetTouch (i).phase == TouchPhase.Ended ) {
                     this.SendMessage ("OnTouchEnded");
                 }    
                 if (Input.GetTouch (i).phase == TouchPhase.Moved ) {
                     this.SendMessage ("OnTouchMoved");
                 }
             }
             ray = Camera.main.ScreenPointToRay (Input.GetTouch (i).position);
             switch (Input.GetTouch (i).phase) {
             case TouchPhase .Began:
                 this.SendMessage ("OnTouchBeganAnywhere");
                 if (Physics.Raycast (ray, out rayHitInfo))
                     rayHitInfo.transform.gameObject.SendMessage ("OnTouchBegan2D");
                 break;
             case TouchPhase .Ended :
                 this.SendMessage ("OnTouchEndedAnywhere");
                 if (Physics.Raycast (ray, out rayHitInfo))
                     rayHitInfo.transform.gameObject.SendMessage ("OnTouchEnded2D");
                 break;
             case TouchPhase .Moved :
                 this.SendMessage ("OnTouchMovedAnywhere");
                 if (Physics.Raycast (ray, out rayHitInfo))
                     rayHitInfo.transform.gameObject.SendMessage ("OnTouchMoved2D");
                 break;
             case TouchPhase .Stationary :
                 this.SendMessage ("OnTouchStayedAnywhere");
                 if (Physics.Raycast (ray, out rayHitInfo))
                     rayHitInfo.transform.gameObject.SendMessage ("OnTouchStayed2D");
                 break;
             }
         }
     }
 }

}

公共类FollowTouch:TouchLogic {

 public float speed=1f;
 private Vector3 finger;
 private Transform myTrans, camTrans;
 void Start () {
     myTrans = this.transform;
     camTrans = Camera.main.transform;
 }
 void LookAtFinger(){
     Vector3 tempTouch=new Vector3 (Input.GetTouch (touch2Watch ).position .x,Input.GetTouch (touch2Watch ).position .y,
         camTrans .position.y-myTrans .position .y);
     finger = Camera.main.ScreenToWorldPoint (tempTouch);
     myTrans.LookAt (finger);
     myTrans.Translate (Vector3.forward * speed * Time.deltaTime);
 }
 void OnTouchMovedAnywhere(){
     LookAtFinger ();
 }
 void OnTouchStayedAnywhere(){
     LookAtFinger ();
 }
 void OnTouchBeganAnywhere(){
     touch2Watch = TouchLogic.currTouch;
 }

1 个答案:

答案 0 :(得分:0)

如果您正在制作2D游戏,则必须使用

Vector3.up

而不是

Vector3.forward