0
因此,我正在开发一款手机游戏,其中我已触摸来自资产商店的资产的输入。除了一件事之外,这一切都很完美:我想实现“拖放”功能,所以当我拿起一个物品时,它应该跟随我的手指位置。但是因为操场是一个球体,所以我必须用Raycast进行运动。所以我的计划是该项目应该遵循Raycasts的立场。我现在的尝试是这样的:Unity Raycast Movement
public void DragTo(float screenX, float screenY)
{
if (draggingObject == null)
{
return;
}
Debug.Log("Dragging");
//Converts the screen space to world space
Vector3 mousePosFar = new Vector3(screenX, screenY, Camera.main.farClipPlane);
Vector3 mousePosNear = new Vector3(screenX, screenY, Camera.main.nearClipPlane);
Vector3 mousePosF = mainCam.ScreenToWorldPoint(mousePosFar);
Vector3 mousePosN = mainCam.ScreenToWorldPoint(mousePosNear);
//shoots an ray to the world space
RaycastHit hit;
if (Physics.Raycast(mousePosN, mousePosF - mousePosN, out hit))
{
float step = speed * Time.deltaTime;
draggingObject.GetComponent<Rigidbody>().MovePosition(hit.point);
}
}
我已经尝试了以下选项:
draggingObject.GetComponent<Rigidbody>().MovePosition(hit.point);
draggingObject.transform.position = hit.point;
draggingObject.transform.position = normal;
他们所有的工作,但他们都有真正的越野车和怪异的行为。另外当我不冻结z。刚体上的物体刚刚在z轴上“飞”,直到到达相机。然后再传送下来,一切都重新开始。所以它在射线上飞起来。所以,如果你有任何想法,为什么发生这种情况让我知道。任何帮助表示赞赏。 (另外不要忘记,它的一切都发生在球体上)
更新: 它现在的作品。当拖动开始时,我将图层从对象改为“忽略Raycast”,并在结束时将其更改回“默认”。