1 using UnityEngine; 2 using System.Collections; 3 4 public class ShouShi : MonoBehaviour 5 { 6 public Transform centerTra;//围绕旋转的中心 7 public Transform targetTra;//移动目标 8 private Vector2 oldPos;//上一帧位置 9 private float angular; 10 private float speed = 0.1f;//升降速度 11 void Update() 12 { 13 if (Input.GetMouseButtonDown(0)) 14 { 15 oldPos = Input.mousePosition; 16 } 17 18 if (Input.GetMouseButton(0)) 19 { 20 Vector3 point = Camera.main.WorldToScreenPoint(centerTra.position );//中心点转换为屏幕坐标 21 angular = SignedAngularGap(new Vector2(point.x, point.y), oldPos, Input.mousePosition); 22 print(angular); 23 float y = targetTra.position.y; 24 y += angular * speed * Time.deltaTime; 25 targetTra.position = new Vector3(targetTra.position.x, y, targetTra.position.z); 26 oldPos = Input.mousePosition; 27 } 28 } 29 30 public static float SignedAngle(Vector2 from, Vector2 to) 31 { 32 // perpendicular dot product 33 float perpDot = (from.x * to.y) - (from.y * to.x); 34 return Mathf.Atan2(perpDot, Vector2.Dot(from, to)); 35 } 36 // return signed angle in degrees between current finger position and ref positions 37 static float SignedAngularGap(Vector2 center, Vector2 oldPos, Vector2 newPos) 38 { 39 Vector2 refDir = (oldPos - center).normalized; 40 Vector2 curDir = (newPos - center).normalized; 41 42 // check if we went past the minimum rotation amount treshold 43 return Mathf.Rad2Deg * SignedAngle(refDir, curDir); 44 } 45 }
原文:http://www.cnblogs.com/yhx8224/p/5243080.html