C# 物体仍在与raycast、don';我不明白为什么

C# 物体仍在与raycast、don';我不明白为什么,c#,unity3d,raycasting,C#,Unity3d,Raycasting,在我的场景中有两辆车,都贴上了汽车的标签。他们有一个剧本叫开车,我不明白为什么他们有时还撞车。这些汽车只有附加的脚本和一个盒子碰撞器。我做错了什么 Ray stopray = new Ray(transform.position, transform.forward); RaycastHit stophit; Ray carray = new Ray(transform.position, transform.forward); RaycastHit carhi

在我的场景中有两辆车,都贴上了汽车的标签。他们有一个剧本叫开车,我不明白为什么他们有时还撞车。这些汽车只有附加的脚本和一个盒子碰撞器。我做错了什么

    Ray stopray = new Ray(transform.position, transform.forward);
    RaycastHit stophit;

    Ray carray = new Ray(transform.position, transform.forward);
    RaycastHit carhit;

    //here we have to slow down
    if (Physics.Raycast(carray, out carhit, 50) && carhit.transform.gameObject.tag == "car")
    {
        if (carhit.distance < 50 && carhit.distance> 12)
        {
            transform.Translate(0, 0, speed / 6 * Time.deltaTime);
        }
    }

    if (Physics.Raycast(carray, out carhit, 12) && carhit.transform.gameObject.tag == "car" || Physics.Raycast(stopray, out stophit, 8) && stophit.transform.gameObject.tag == "stop")
    {
        // Here we have to stop
        if (carhit.distance < 12)
        {
            transform.Translate(0, 0, 0);
        }

    }
    else
    {
        transform.Translate(0, 0, speed * Time.deltaTime);
    }
光线停止光线=新光线(transform.position,transform.forward);
雷卡斯特停击;
光线carray=新光线(transform.position,transform.forward);
雷卡斯特·卡希特;
//我们必须放慢速度
if(Physics.Raycast(carray,out carhit,50)和&carhit.transform.gameObject.tag==“car”)
{
if(carhit.distance<50和&carhit.distance>12)
{
transform.Translate(0,0,速度/6*Time.deltaTime);
}
}
if(Physics.Raycast(carray,out-carhit,12)和&carhit.transform.gameObject.tag==“car”| | Physics.Raycast(stopray,out-stophit,8)和&stophit.transform.gameObject.tag==“stop”)
{
//我们必须停下来
如果(车辆碰撞距离<12)
{
transform.Translate(0,0,0);
}
}
其他的
{
transform.Translate(0,0,速度*时间.deltaTime);
}
(地图上还有不可见的停止立方体) 结果是这样的,这不应该发生:

问题已解决:光线进入汽车下方,但从未见过,解决方案:

        Ray carray = new Ray(new Vector3 (transform.position.x, transform.position.y +1, transform.position.z), transform.forward);
    RaycastHit carhit;

光线现在稍微高了一点,可以看到汽车。

检查以确保光线投射的方向与您认为的方向一致。只要在光线投射命中某个对象时抛出一条“调试”行即可。我建议制作一个空白场景,然后只导入两辆汽车和它们所依赖的任何“地面”来测试这一点

在某个地方(我现在似乎找不到)有文档说明如何使光线投射在编辑器中实际绘制为红线,以便您可以看到发生了什么


而且,根据你想要什么样的行为,你可能会考虑制造一个触发器对撞机,而不是一个光线投射器来检测汽车之间的距离是否太近。

< P>我建议你根据你的条件引入局部速度变量并初始化它,然后在所有条件下只调用一次。检查试试这个:

Ray stopray = new Ray(transform.position, transform.forward);
RaycastHit stophit;

Ray carray = new Ray(transform.position, transform.forward);
RaycastHit carhit;

// speed for the current frame
var currentSpeed = speed;

//here we have to slow down
if (Physics.Raycast(carray, out carhit, 50) && carhit.transform.gameObject.tag == "car")
{
    if (carhit.distance < 50 && carhit.distance> 12)
    {
        currentSpeed = speed / 6;
    }
}

if (Physics.Raycast(carray, out carhit, 12) && carhit.transform.gameObject.tag == "car" || Physics.Raycast(stopray, out stophit, 8) && stophit.transform.gameObject.tag == "stop")
{
    // Here we have to stop
    if (carhit.distance < 12)
    {
        currentSpeed = 0f;
    }
}

// move the car with right speed
transform.Translate(0, 0, currentSpeed * Time.deltaTime);
光线停止光线=新光线(transform.position,transform.forward);
雷卡斯特停击;
光线carray=新光线(transform.position,transform.forward);
雷卡斯特·卡希特;
//当前帧的速度
var currentSpeed=速度;
//我们必须放慢速度
if(Physics.Raycast(carray,out carhit,50)和&carhit.transform.gameObject.tag==“car”)
{
if(carhit.distance<50和&carhit.distance>12)
{
当前速度=速度/6;
}
}
if(Physics.Raycast(carray,out-carhit,12)和&carhit.transform.gameObject.tag==“car”| | Physics.Raycast(stopray,out-stophit,8)和&stophit.transform.gameObject.tag==“stop”)
{
//我们必须停下来
如果(车辆碰撞距离<12)
{
电流速度=0f;
}
}
//以正确的速度移动汽车
transform.Translate(0,0,currentSpeed*Time.deltaTime);

能否添加调试信息以检查碰撞对象?这是一个有点难以猜测,只有这一部分的游戏。这样做,但汽车仍然崩溃。我还添加了
Vector3 forward=transform.TransformDirection(Vector3.forward)*30;Debug.DrawRay(transform.position、forward、Color.green)但没什么可看的。如果我放置一个
Debug.Log(carhit.transform.gameObject.tag)
它只显示停止符号(
Debug.Log(stophit…