Unity 2D Raycast Refection无法正常工作

时间:2019-03-05 15:42:46

标签: c# unity3d game-physics

我想使用使用2d物理的线渲染器创建反射轨迹。 感染有时无法正常运行,有时无法正常运行。 对于3d,只要将vector2替换为vecor3并使用3d物理函数,一切都可以正常工作。

也许它与对撞机有关,否则我会在彩色块上添加盒式对撞机2d。

我写了他的代码

void GenerateRay(Vector2 position , Vector2 direction , int count){

    if(count==0) return;

    Vector2 oldPosition = position;

    RaycastHit2D hit = Physics2D.Raycast(position , direction , maxRayLenght);

    if(hit.collider!=null){

        direction = Vector2.Reflect(direction , hit.normal);
       print(direction);
        position = hit.point;
         print(position);

    }
    else
    {
        position += direction*maxRayLenght;
    }


    lineRenderInstance[maxHop-count].SetPosition(0 , oldPosition);
    lineRenderInstance[maxHop-count].SetPosition(1 , position);



    GenerateRay(position , direction , count-1 );
}

如果我改变y距离,反射有时会起作用,有时甚至不会。[!

非工作案例

enter image description here

工作情况,如果改变高度 enter image description here

enter image description here

0 个答案:

没有答案
相关问题