放大缩小物体

 1  Vector2 oldPos1;//第一个手指
 2     Vector2 oldPos2;//第二个手指
 3 
 4     void Update()
 5     {
 6         //如果有2个手指
 7         if (Input.touchCount == 2)
 8         {
 9             //如果第一个手指位置是移动的或者第二个手指位置是移动的
10             if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
11             {
12                 Vector2 newPos1 = Input.GetTouch(0).position;
13                 Vector2 newPos2 = Input.GetTouch(1).position;
14                 if (isEnLarge(oldPos1, oldPos2, newPos1, newPos2))
15                 {
16                     float oldScale = transform.localScale.x;
17                     float newScale = oldScale * 1.025f;
18                     transform.localScale = new Vector3(newScale, newScale, newScale);
19 
20                 }
21                 else
22                 {
23                     float oldScale = transform.localScale.x;
24                     float newScale = oldScale / 1.025f;
25                     transform.localScale = new Vector3(newScale, newScale, newScale);
26 
27                 }
28                 oldPos1 = newPos1;
29                 oldPos2 = newPos2;
30             }
31         }
32     }
33 
34     //判断手势
35     bool isEnLarge(Vector2 oldP1, Vector2 oldP2, Vector2 newP1, Vector2 newP2)
36     {
37         float length1 = Mathf.Sqrt((oldP1.x - oldP2.x) * (oldP1.x - oldP2.x) + (oldP1.y - oldP2.y) * (oldP1.y - oldP2.y));
38         float length2 = Mathf.Sqrt((newP1.x - newP2.x) * (newP1.x - newP2.x) + (newP1.y - newP2.y) * (newP1.y - newP2.y));
39 
40         //放大
41         if (length1 < length2)
42         {
43             return true;
44         }
45         //缩小
46         else
47         {
48             return false;
49         }
50     }

 

posted on 2018-09-18 17:35  追求LPY  阅读(171)  评论(0编辑  收藏  举报