unity 單指雙指事件(單指點擊移動,雙指滑動拖放)


  1 using System.Collections;
  2 using System.Collections.Generic;
  3 using UnityEngine;
  4 
  5 public class TouchControl : MonoBehaviour {
  6 
  7     private Vector3 startFingerPos;
  8     private Vector3 endFingerPos;
  9     private float xMoveDistance;
 10     private float yMoveDistance;
 11     private int backValue = 0;
 12 
 13     private Vector3 target;
 14     private Vector3 offSet;
 15     private bool isOver = true;
 16 
 17     void FingerRotation()
 18     {
 19 
 20         xMoveDistance = Mathf.Abs(endFingerPos.x - startFingerPos.x);
 21         yMoveDistance = Mathf.Abs(endFingerPos.y - startFingerPos.y);
 22 
 23         if (xMoveDistance > yMoveDistance)
 24         {
 25 
 26             if (endFingerPos.x - startFingerPos.x > 0)
 27             {
 28                 backValue = -1; //沿着X軸負方向移動  
 29             }
 30             else
 31             {
 32                 backValue = 1; //沿着X軸正方向移動  
 33             }
 34 
 35         }
 36         if (backValue == -1)
 37         {
 38             transform.Rotate(Vector3.down * Time.deltaTime * 150, Space.Self);
 39         }
 40         else if (backValue == 1)
 41         {
 42             transform.Rotate(Vector3.up * Time.deltaTime * 150, Space.Self);
 43         }
 44 
 45 
 46     }
 47 
 48     void MoveTo(Vector3 tar)
 49     {
 50         if (!isOver)
 51         {
 52             Vector3 offSet = tar - transform.position;
 53             transform.position += offSet.normalized * 5 * Time.deltaTime;
 54             if (Vector3.Distance(tar, transform.position) < 0.5f)
 55             {
 56                 isOver = true;
 57                 transform.position = tar;
 58             }
 59         }
 60 
 61     }
 62 
 63 
 64     // Use this for initialization
 65     void Start()
 66     {
 67 
 68     }
 69 
 70     // Update is called once per frame
 71     void Update()
 72     {
 73 
 74         //沒有觸摸  
 75         if (Input.touchCount <= 0)
 76         {
 77             //MoveTo(target);
 78         }
 79 
 80         if(Input.touchCount==1)//單指操作
 81         {
 82             Touch t1 = Input.GetTouch(0);
 83             if ( t1.phase==TouchPhase.Began)
 84             {
 85                 //1. 獲取鼠標點擊位置
 86                 //創建射線;從攝像機發射一條經過鼠標當前位置的射線
 87                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//Input.GetTouch(0).position);
 88                                                                             //發射射線
 89                 RaycastHit hitInfo = new RaycastHit();
 90                 if (Physics.Raycast(ray, out hitInfo))
 91                 {
 92                     //獲取碰撞點的位置
 93                     if (hitInfo.collider.name == "Plane")
 94                     {
 95                         target = hitInfo.point;
 96                         target.y = 0.5f;
 97                         isOver = false;
 98                     }
 99                 }
100                 transform.LookAt(target);
101             }
102             //2. 讓角色移動到目標位置
103             MoveTo(target);
104         }
105         else if(Input.touchCount>1)//多指操作
106         {
107             Touch t1 = Input.GetTouch(0);
108             Touch t2 = Input.GetTouch(1);
109             //單點觸控記錄初始點
110             if (t1.phase == TouchPhase.Began)
111             {
112                 startFingerPos = t2.position;
113             }
114 
115             endFingerPos = t2.position;//實時手指位置 
116 
117             if ((t1.phase == TouchPhase.Moved)&& (t2.phase == TouchPhase.Moved)) //雙指滑動進行物體旋轉
118             {
119                 FingerRotation();
120                 return;
121             }//物體旋轉
122 
123         }
124         MoveTo(target);
125     }
126 }

Touch.position 是 一個 像素坐標(手機左下角為(0,0))

Touch.phase {Began,Moved,Stationary,Canceled,Ended} 代表 手指點擊的狀態

每次手指點擊會在input.touches[]增加一個touch實例,記錄手指的狀態信息,當有一個手指退出屏幕,那么數組中的位置產生一個空缺,

當有新手指加入時候,新手指將替代剛剛退出的手指在數組中的位置。記得在手指退出屏幕時候,將手指所綁定的跟蹤信息重新初始化。

Input.touchCount 觸摸隨之增長,一秒50次增量。

Input.GetTouch(0).phase==TouchPhase.Moved 手指滑動中最后一幀滑動的狀態是運動的。

TouchPhase  觸摸的幾個狀態。

Touch.deltaPosition 增量位置(Input.GetTouch(0).deltaPosition)最后一幀滑動的值,只返回xy軸坐標,也可用vector3(z軸為0),所以一般用vector2接收。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM