两种模式:
1.Screen Space-Overlay
public void OnDrag(PointerEventData eventData){ Vector2 targetDir = eventData.position - new Vector2(transform.position.x, transform.position.y); // 目标坐标与当前坐标差的向量 float ang = Vector2.Angle(Vector3.right, targetDir); // 返回当前坐标与目标坐标的角度 if (eventData.position.y
2.Screen Space-Camera
public void OnDrag(PointerEventData eventData) { Camera camera = GameObject.Find("Main Camera").GetComponent(); Vector3 screenPos = camera.WorldToScreenPoint(transform.position);//世界坐标转屏幕坐标 Vector2 targetDir = eventData.position - new Vector2(screenPos.x, screenPos.y);// 目标坐标与当前坐标差的向量float ang = Vector2.Angle(Vector3.right, targetDir); // 返回当前坐标与目标坐标的角度 if (eventData.position.y < screenPos.y) { ang = 360 - ang; } transform.localEulerAngles = new Vector3(0, 0, ang); }