使用raycast停止对象

时间:2018-05-07 04:11:04

标签: c# unity3d raycasting

所以我有简单的物体(相机),我不想让她穿过墙壁。我所做的是简单的光线投射编码:

RaycastHit hit;
if (Physics.Raycast(myCamera.transform.position, Vector3.down, out hit, distanceFromMesh))
{
    currPos.y = hit.point.y + distanceFromMesh;
}
if (Physics.Raycast(myCamera.transform.position, Vector3.up, out hit, distanceFromMesh))
{
    currPos.y = hit.point.y - distanceFromMesh;
}
if (Physics.Raycast(myCamera.transform.position, Vector3.left, out hit, distanceFromMesh))
{
    currPos.x = hit.point.x - distanceFromMesh;
}
if (Physics.Raycast(myCamera.transform.position, Vector3.right, out hit, distanceFromMesh))
{
    currPos.x = hit.point.x + distanceFromMesh;
}

当我将相机缓慢地移动到墙上时(这与普通人一样),这种效果很好,但是当我将它移到墙上时,它会通过并被卡在里面(因为光线检查检查其他侧移动。

我能做些什么来阻止这种情况发生。我的distanceFromMesh = 0.2f

0 个答案:

没有答案