Unity3D判断触摸方向
据说
Temple Run(神庙逃亡)
就是用这种方式操作的
废话不多说
直接上代码
using UnityEngine; using System.Collections; public class Touch : MonoBehaviour { private Vector2 beginPos; // Use this for initialization void Start() { } // Update is called once per frame void Update() { TouchDirection(); } void TouchDirection() { if (Input.touchCount <= 0) return; if (Input.touchCount == 1) { if (Input.touches[0].phase == TouchPhase.Began) { beginPos = Input.touches[0].positon; } else if (Input.touches[0].phase == TouchPhase.Moved) { } if (Input.touches[0].phase == TouchPhase.Ended && Input.touches[0].phase != TouchPhase.Canceled) { Vector2 pos = Input.touches[0].position; if (Mathf.Abs(beginPos.x = pos.x) > Mathf.Abs(beginPos.y = pos.y)) { if (beginPos.x > pos.x) { //向左 } else { //向右 } } else { if (beginPos.y > pos.y) { //向下 } else { //向上 } } } } } }
声明:此博客为个人学习之用,如与其他作品雷同,纯属巧合,并请明示指出