简体   繁体   English

Unity Raycast问题

[英]Unity Raycast problems

Well I'm trying to make a circle(with dynamic position) and another circle that SHOULD not pass through the first one --- like in the image below 好吧,我正在尝试制作一个圆(具有动态位置),另一个圆应该不通过第一个圆---如下图所示

The reason why I am doing this way is because there can be a lot of other objects on the big one. 我之所以这样做,是因为大型对象上可能还有许多其他对象。

圆圈图片

but the problem is that Physics.Raycast is not always detecting my sphere: 但问题是Physics.Raycast并不总是检测我的球体:

虚线圆图像

the blue lines are raycasts 蓝线是射线

Basically what i do is raycast through the whole big circle and find out where the smaller one is. 基本上,我的工作是通过整个大圈子进行射线广播,并找出较小的圈子在哪里。

Here is my code for detecting objects on the big circle: 这是我用于检测大圆上物体的代码:

void GetCollisions () {
    Angle FixedRenderAngle, k;
    RaycastHit hit;
    Vector3 p1, p2;
    float d, d1, radius;

    for ( int i = 0; i < Radiuses.Count; i++ ) {
        if ( Angles[i] == null )
            Angles[i] = new List<Angle>();

        radius = Radiuses[i];
        FixedRenderAngle = 360f / (float)NumberOfCalls;
        AnglesLast[i] = 0;
        Angle j = 0f, pj;
        bool exception = false;

        for ( int l = 0; l < NumberOfCalls; l++) {
            j+= FixedRenderAngle;
            pj = j - FixedRenderAngle;
            p1 = pj.PointByRadius ( radius );
            p2 = j.PointByRadius ( radius );
            Debug.DrawRay ( p1, p2 - p1, Color.cyan, 3f );
            if ( Physics.Raycast ( p1, p2 - p1, out hit, Vector3.Distance ( p1, p2 ) ) ) {
                d = Vector3.Distance ( p1, p2 );
                d1 = Vector3.Distance ( p1,hit.point );
                k =  pj + (j -pj) * (d1/d); 

                AddToAngles ( ref k, i );
            }

            if ( Physics.Raycast ( p2, p1 - p2, out hit, Vector3.Distance ( p1, p2 ) ) ) {
                if ( AnglesLast[i] == 0 ) exception = true;

                d = Vector3.Distance ( p1, p2 );
                d1 = Vector3.Distance ( p1,hit.point );
                k =  pj + (j -pj) * (d1/d); 

                AddToAngles ( ref k, i );
            }
        }

        if ( exception == true ) {
            Angle tmp = Angles[i][AnglesLast[i] - 1];
            for ( int l = AnglesLast[i] - 1; l > 0 ; l-- )
                Angles[i][l] = Angles[i][l-1];
            Angles[i][0] = tmp;
        }
    }
}

Angle is a class with float numbers representing degrees(0 to 360) and it's function PointByRadius is returning a Vector3 representing the location where should it be on a circle based on the angle and with a given radius. Angle是一个用浮点数表示度数(0到360)的类,它的功能PointByRadius返回一个Vector3,该向量表示基于角度和给定半径的圆上的位置。

Did I do something wrong or there is something that i don't know about raycasts? 我做错了什么,或者关于射线广播我不知道什么? Any help would be appreciated. 任何帮助,将不胜感激。 (Of course any other method for this would help) (当然,任何其他方法都可以帮助)

Physics.SphereCast ? Physics.SphereCast It's somewhat like OverlapSphere but maybe more what you need? 它有点像OverlapSphere,但也许您还需要更多? (it's kinda hard to tell exactly what you're trying to do though... eg when you say circle, do you mean actual circles moving only on XY or they moving through Z? which circle are you trying to hide things in, the big or small one? are you trying to visually hide a segment of the larger circle?) (虽然很难准确地说出您要尝试做的事情。例如,当您说圆圈时,您是指实际的圆圈仅在XY上移动还是在Z上移动?您要隐藏哪个圆圈,大还是小?您是要在视觉上隐藏较大圆圈的一部分吗?)

Depending on how fast the objects are moving and sizes, raycasts (and PhysX colliders in general) can get wonky, at least in my experience. 视物体移动的速度和大小而定,至少在我的经验中,射线投射(通常是Phy​​sX碰撞器)可能会变得不稳定。 You might also try messing with your physics timestep lengths to see if that's part of the problem, like do it more often and see if you still get the problem behavior. 您还可以尝试弄乱物理时间步长,以查看这是否是问题的一部分,例如更频繁地进行操作,并查看是否仍然存在问题。 Not sure, just throwing ideas out. 不知道,只是丢掉想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM