新建一個Cube物體,創建一個腳本TouchTest04,將該腳本掛到Cube物體上,代碼如下:
- using UnityEngine;
- using System.Collections;
- public class TouchTest04 : MonoBehaviour
- {
- void Update()
- {
- int touchNum = Input.touchCount;
- if (touchNum > 0)
- {
- Touch touch = Input.GetTouch(0);
- if (touch.phase == TouchPhase.Moved)
- {
- Vector3 dir = new Vector3(touch.deltaPosition.x, touch.deltaPosition.y, 0f) * 0.1f;
- transform.Translate(dir);
- }
- }
- }
- }
OK,完成!