【问题标题】:Touch and Drag Issues Unity 2D Game触摸和拖动问题 Unity 2D 游戏
【发布时间】:2016-04-16 15:42:17
【问题描述】:

我的触控有一些问题。

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

【问题讨论】:

    标签: android touch 2d unity5


    【解决方案1】:

    如果你正在制作 2d 游戏,你必须使用

    Vector3.up
    

    而不是

    Vector3.forward
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多