【发布时间】:2018-12-22 11:49:15
【问题描述】:
我想在屏幕上左右滑动时旋转一个球,所以我做了下面的脚本,但是只有当我直接在球上滑动而不是屏幕上的任何位置时,球才会旋转,你知道吗?
球脚本:
private float baseAngle = 0.0f;
public Camera cam;
void OnMouseDown()
{
Vector3 pos = cam.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
}
void OnMouseDrag()
{
Vector3 pos = cam.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg - baseAngle;
transform.rotation = Quaternion.AngleAxis(-ang, Vector3.up);
}
【问题讨论】: