2016-04-16 42 views
1

我的觸摸控制有幾個問題。觸摸和拖動問題Unity 2D遊戲

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

回答

0

如果您正在製作的2D遊戲,你必須使用

Vector3.up 

,而不是

Vector3.forward