2

I can't see a problem in code but in game if there is mirror anywhere in front of the beam it ignores all other objects

Please can anyone help me, why this is happening.

I can't ahre this because "It's all code and stuff".

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 I can't see a problem in code but in game if there is mirror anywhere in front of the beam it ignores all other objects

Please can anyone help me, why this is happening.

I can't ahre this because "It's all code and stuff".

6
  • Ok. Morrror is a 3D model. What are you actually doing? If you want mirror to be ignored, you can put it in a different layer and modify your Physics.RaycastAll to exclude that layer.
    – Programmer
    Commented May 14, 2016 at 17:17
  • No, I don't want to mirror to be ignored, i want other objects to don't be ignored when there is a mirror in the same direction ut laser just ignores other objects if there is a mirror. @Programmer Commented May 14, 2016 at 17:19
  • @KodeKishin you need to learn about Layers in Unity.
    – Fattie
    Commented May 14, 2016 at 17:30
  • @JoeBlow they are all in same layer. Commented May 14, 2016 at 17:33
  • ok, very few video games use "all the same layer". you typically have many, many different layers. that is the normal situation in games. (many engineers put every object on its own layer) it should then be easy to achieve whatever it is you are trying to do.
    – Fattie
    Commented May 14, 2016 at 17:36

1 Answer 1

1

I don't think you can get away with layers in this case, but you DO NEED to know how to use them, anyways, this should work:

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; 
                }
}

sorry for bad formatting, I'm on windows :/

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.