视觉辅助设备
厚度与宽度:这里
请查看简短的gif。
这里的厚度和宽度是不同的,因为有多个墙壁,因为有外部和内部圆柱。厚度是测量圆柱体任何一侧的外壁/内壁之间的距离,而厚度是指一端到另一端的距离,其中包含中空空间之间的距离。
关于gifs的快速概要提供了
-On每次单击原点(蓝色)和目标点(橙色)球都是为了表示用户单击的位置和用于计算距离(显示在GUI上)的解释结束点。
原点定义用户单击物体对撞机表面的位置,目标定义与原点的世界Y轴垂直的点,其中第二射线投射到对撞机的另一侧。
当前:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//obtain the vector where the ray hit the collider.
hitPoint = hit.point; //origin point
//offset the ray, keeping it along the XZ plane of the hit
Vector3 offsetDirection = -1 * hit.normal;
//offset a long way, minimum thickness of the object
ray.origin = hit.point + offsetDirection * 100;
//point the ray back at the first hit point
ray.direction = (hit.point - ray.origin).normalized;
//raycast all, because there might be other objects in the way
RaycastHit[] hits = Physics.RaycastAll(ray);
foreach (RaycastHit h in hits)
{
if (h.collider == hit.collider)
{
hitBack = h.point; //destination point
}
}
}当前,宽度是适当的功能。我想要计算厚度,而不必进入对象内部(如gif中所示)。
超凡参考
http://answers.unity3d.com/questions/386698/detecting-how-many-times-a-raycast-collides-with-a.html
这个家伙基本上和我有同样的问题,并且有一个可能有效的解决方案。我不知道线播和雷射是如何工作的。
发布于 2017-08-11 19:06:34
保留:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//obtain the vector where the ray hit the collider.
hitPoint = hit.point; //origin point
//offset the ray, keeping it along the XZ plane of the hit
Vector3 offsetDirection = -1 * hit.normal;
//offset a long way, minimum thickness of the object
ray.origin = hit.point + offsetDirection * 100;
//point the ray back at the first hit point
ray.direction = (hit.point - ray.origin).normalized;取代:
//raycast all, because there might be other objects in the way
RaycastHit[] hits = Physics.RaycastAll(ray);
foreach (RaycastHit h in hits)
{
if (h.collider == hit.collider)
{
hitBack = h.point; //destination point
}
}(归功于镜镜颇有洞察力的帖子,@ryemoss提供了他的工具性建议和帮助):
int counter = 0;
bool calculating = false; //set this to true on click
Vector3 Point, PreviousPoint, Goal, Direction;
Point = ray.origin;
Goal = hit.point;
Direction = ray.direction;
PreviousPoint = Vector3.zero;
while (calculating == true)
{
counter++;
RaycastHit hit2;
if (Physics.Linecast(Point, Goal, out hit2))
{
if(counter > 100)
{
hitBack = hitPoint;
counter = 0;
calculating = false;
break;
}
PreviousPoint = hit2.point;
Point = hit2.point + (Direction / 10000f);
}
else
{
if (PreviousPoint == Vector3.zero)
hitBack = hitPoint;
else
hitBack = PreviousPoint;
calculating = false;
counter = 0;
}
}Linecast vs Raycast
通过光线投射,你可以设置开始点、方向和距离来检查这个方向,用一个直线广播你只需设置开始点和结束点,它就会在这两个点之间进行检查。
因此,如果您具体知道最终目的地,使用linecast,如果您想检查一个特定的方向,但没有特定的终点,使用射线广播。
溶液
首先,使用初始的raycast获得第一个点,hit.point。然后,将ray.origin设置为对撞机外世界空间中的一个点(我们首先与之碰撞的对象的对撞机以获得hit.point),并将ray.direction设置为在第一个点( hit.point )面对光线。
最后,使用with循环在ray.origins新位置创建一个新的行广播(每次通过while循环更新,直到linecast到达hit.point为止),每次发生与对象的冲突,直到linecast到达hit.point为止。一旦到达hit.point,就意味着对象的每个表面都被击中,在每次命中时,都会创建一条新的行,直到一条线到达第一个起始点hit.point。若要计算厚度,请取第一次命中,hit.point和反向直线广播之前的命中距离,hit.point,PreviousPoint。
更新
1-修改代码以正确处理单边对象(例如:平面)。
2-增加计数器,以防止计算不可能的特殊情况。
3-提高可读性。
https://stackoverflow.com/questions/45576911
复制相似问题