c# - 如果有镜面激光忽略其他物体

标签 c# unity3d

我在代码中看不出问题,但在游戏中,如果光束前面的任何地方有镜子,它会忽略所有其他物体

请任何人帮助我,为什么会这样。

我看不懂,因为“全是代码之类的东西”。

public class LaserBeam : MonoBehaviour {

    LineRenderer lr;
    public bool isOpen = true;
    Vector3 s;
    void Start () {
        lr = GetComponent<LineRenderer>();
    }

    void Update () {

        s = new Vector3(transform.position.x, transform.position.y + (2 / 5f), transform.position.z);
        lr.SetPosition(0, s);
        lr.SetWidth(0.3f, 0.3f);
        if (isOpen)
        {
            RaycastHit[] Hit = Physics.RaycastAll(s, transform.forward, 100.0F);

            //Debug.Log("isOpen W");

            if (Hit.Length > 0)
            {
                for (int x = 0; x < Hit.Length; x++)
                {
                    Debug.Log(Hit[x].collider.tag + " ID: " + x);
                    if (Hit[x].collider.tag == "Mirror" || !Hit[x].collider.isTrigger)
                    {
                        Debug.DrawLine(s, Hit[x].point, Color.blue);
                        lr.SetPosition(1, Hit[x].point);
                       // Debug.Log("loop W" + x);
                        if (Hit[x].collider.tag == "Mirror") Reflect(s, Hit[x], 0);
                        else lr.SetVertexCount(2);
                        break; 
                    }
                    else if (x == Hit.Length - 1)
                    { 
                        lr.SetVertexCount(2);
                        Debug.DrawLine(s, transform.forward * Int16.MaxValue, Color.blue);
                        lr.SetPosition(1, transform.forward * Int16.MaxValue);
                        break;
                    }
                }
            }
            else
            {
                lr.SetVertexCount(2);
                Debug.DrawLine(s, transform.forward * Int16.MaxValue, Color.blue);
                lr.SetPosition(1, transform.forward * Int16.MaxValue);
            }
        }
        else
        {
            lr.SetVertexCount(2);
            lr.SetPosition(1, s);
        }
    }


    public void Reflect(Vector3 start, RaycastHit hit, int id)
    {
        lr.SetVertexCount(id + 3);
        Vector3 p = Vector3.Reflect(hit.point - start, hit.normal);
        Debug.DrawRay(hit.point, hit.normal * 3);
        Debug.DrawLine(hit.point, p + hit.point, Color.blue);
        RaycastHit[] Hit1 = Physics.RaycastAll(hit.point, p, 100.0F);
        if (Hit1.Length > 0)
        {
            for (int x = 0; x < Hit1.Length; x++)
            {
                if (Hit1[x].collider.tag == "Mirror" || !Hit1[x].collider.isTrigger)
                {
                    Debug.DrawLine(hit.point, Hit1[x].point, Color.blue);
                    //Debug.DrawLine(hit.point, Hit[x].point, Color.blue);
                    //lr.SetPosition(id + 1,(hit.point + start) / 2);
                    //lr.SetPosition(id + 2, hit.point);
                    lr.SetPosition(id + 2, Hit1[x].point);

                    if (Hit1[x].collider.tag == "Mirror")
                    {
                        Reflect(hit.point, Hit1[x], (id + 1));
                        return;
                    }
                    else lr.SetVertexCount(id + 3);
                return; 
                }
                else if (x == Hit1.Length - 1)
                {
                    lr.SetVertexCount(id + 3);
                    Debug.DrawLine(hit.point, Vector3.Normalize(p) * Int16.MaxValue, Color.blue);
                    //lr.SetPosition(id + 1, (hit.point + start) / 2);
                    //lr.SetPosition(id + 2, hit.point);
                    lr.SetPosition(id + 2, Vector3.Normalize(p) * Int16.MaxValue);
                    return;
                }
                return;
            }
        }
        else
        {
            lr.SetVertexCount(id + 3);
            Debug.DrawLine(hit.point, Vector3.Normalize(p) * Int16.MaxValue, Color.blue);
            //lr.SetPosition(id + 1, (hit.point + start) / 2);
            //lr.SetPosition(id + 2, hit.point);
            lr.SetPosition(id + 2, Vector3.Normalize(p) * Int16.MaxValue);
            return;
        }
      //  Debug.Log(id);
    }
}

enter image description here enter image description here 我在代码中看不到问题,但在游戏中,如果光束前面的任何地方都有镜子,它会忽略所有其他对象

请任何人帮助我,为什么会这样。

我看不懂,因为“全是代码之类的东西”。

最佳答案

我认为在这种情况下您无法摆脱层,但您确实需要知道如何使用它们,无论如何,这应该有效:

if (Hit.Length > 0)
{
    float firstHitDistance = 100000f; //or something ridiculously high
    RaycastHit firstHit;
    for (int x = 0; x < Hit.Length; x++)
    {
        if(Hit[x].distance < firstHitDistance){
            firstHitDistance = Hit[x].distance;
            firstHit = Hit[x];
        }
    }

                if (firstHit.collider.tag  == "Mirror" || !firstHit.collider.isTrigger)
                {
                    Debug.DrawLine(s, firstHit.point, Color.blue);
                    lr.SetPosition(1, firstHit.point);
                   // Debug.Log("loop W" + x);
                    if (firstHit.collider.tag == "Mirror") Reflect(s, , 0);
                    else lr.SetVertexCount(2);
                    break; 
                }
}

抱歉格式错误,我在 Windows 上:/

关于c# - 如果有镜面激光忽略其他物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37229357/

相关文章:

c# - 从十六进制获取颜色

c# - Entity Framework 以一对多方式替换集合的正确方法

c# - 向 ASP.NET Identity 中的 ApplicationUser 类添加关系(数据库优先)

c# - 角色自动移动时卡住

unity3d - (Unity + 2D) 更改 UI 按钮位置问题

c# - 与此 C# 片段等效的 UnityScript

c# - 使用带锁的异步调用减慢速度

c# - MVVM - 如何使格式化属性保持最新?

unity3d - 使用 Everyplay 插件的 Unity 云构建

c# - Unity/C# 游戏存档迁移