Unity3D之觸摸輸入實現物體滑動

新建一個Cube物體,創建一個腳本TouchTest04,將該腳本掛到Cube物體上,代碼如下:

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class TouchTest04 : MonoBehaviour   
  5. {  
  6.     void Update()  
  7.     {  
  8.         int touchNum = Input.touchCount;  
  9.   
  10.         if (touchNum > 0)  
  11.         {  
  12.             Touch touch = Input.GetTouch(0);  
  13.             if (touch.phase == TouchPhase.Moved)  
  14.             {  
  15.                 Vector3 dir = new Vector3(touch.deltaPosition.x, touch.deltaPosition.y, 0f) * 0.1f;  
  16.                 transform.Translate(dir);  
  17.             }  
  18.         }  
  19.     }  
  20. }  

OK,完成!
發佈了56 篇原創文章 · 獲贊 61 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章