using UnityEngine;

public class ReflectionRay : MonoBehaviour {

    private static RaycastHit hit;
    private static float rayDistance = 25f;

    void Update() {
        bool hitObjectFirst = Physics.Raycast(transform.position, Camera.main.transform.forward, out hit, rayDistance);
        if (!hitObjectFirst) {
            Debug.DrawRay(transform.position, Camera.main.transform.forward * rayDistance, Color.blue);
        } else {
            Vector3 firstHitPoint = hit.point;
            Debug.DrawRay(transform.position, firstHitPoint - transform.position, Color.blue);
            Vector3 inDirection = Vector3.Reflect(Camera.main.transform.forward, hit.normal);
            bool hitObjectSecond = Physics.Raycast(firstHitPoint + hit.normal * 0.01f, inDirection, out hit, rayDistance);
            if (!hitObjectSecond) {
                Debug.DrawRay(hit.point, inDirection * rayDistance, Color.red);
            } else {
                Vector3 secondHitPoint = hit.point;
                Debug.DrawRay(firstHitPoint, secondHitPoint - firstHitPoint, Color.red);
                inDirection = Vector3.Reflect(inDirection, hit.normal);
                bool hitObjectThird = Physics.Raycast(secondHitPoint + hit.normal * 0.01f, inDirection, out hit, rayDistance);
                if (!hitObjectThird) {
                    Debug.DrawRay(secondHitPoint, inDirection * rayDistance, Color.green);
                } else {
                    Vector3 thirdHitPoint = hit.point;
                    Debug.DrawRay(secondHitPoint, thirdHitPoint - secondHitPoint, Color.green);
                }
            }
        }
    }
}