【发布时间】:2019-06-06 18:21:02
【问题描述】:
当使用光线投射缓慢拖动对象时,对象的移动会有一点延迟。
当快速拖动对象时,鼠标指针会离开图层光线投射的区域,因此对象不再移动。
主要项目是在平面上拖动一个立方体。
但为了让项目更简单,我打开了一个新的 2D 项目并制作了一个圆圈并为其分配了以下脚本,并在其上附加了一个球体对撞机(主要目标在 3D 空间中)。
// If some one wrote:
private Vector2 deltaPos;
void Update () {
Vector2 touchPos;
if (Input.GetMouseButtonDown (0)) { // Clicking the Target
RaycastHit hit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
touchPos = new Vector3 (hit.point.x, hit.point.y);
deltaPos.x = touchPos.x - transform.position.x;
deltaPos.y = touchPos.y - transform.position.y;
Debug.Log ("You Clicked Me");
}
}
if (Input.GetMouseButton (0)) {
RaycastHit hit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
transform.position = new Vector2 (hit.point.x - deltaPos.x, hit.point.y - deltaPos.y);
}
}
}
我希望有规律地拖动球体,但结果是指针在快速移动时超出了球体对撞机,因此圆停止移动。
我找到了这个 article,然后我将 void 从 Update() 更改为 FixedUpdate(),但结果相同。
【问题讨论】:
-
您是否尝试过使用IDragHandler 和IBeginDragHandler?是否给您相同的结果?
-
不,我尽量让我的代码简单易懂。
标签: unity3d raycasting