is my Raycast being reflected correctly and why is it giving me an infinite loop?
I'm trying to build a light source that emits light from it's position and to the forward direction, which is visualized using a Line Renderer, and using RayCast() I want to detect weather or not it collides with an object and if so I want to reflect the light and on it goes
my implementation so far, which is causing me an infinite while loop
void Update () {
//for debugging purposes count how many reflections I get
loopCount = 0;
//a Rays object simply hold two Vector3 values, a starting position and a direction
Rays ray = new Rays(startPos.position, startPos.forward);//
while (Physics.Raycast(ray.rayPosition, ray.rayDirection, out hit, distance)) {
//in case it hits then I want to store the position of the collider and its direction
//to be used in the next Raycast itteration
Vector3 reflection = Vector3.Reflect(ray.rayDirection, hit.normal);
ray = new Rays(hit.point, reflection);
loopCount++;
}
Debug.Log(loopCount);
I can't figure out why I'm getting an infinite loop , unless my new rays are reflecting in correctly so that I'm always getting a true value from my Raycast()
Well, to debug I'd start by changing your while (...)
to a while (... && loopCount < 10)
and them output the value of ray on each iteration to see if it's what you expected.
thank you! I know why I was getting an infinite loop... apparently the object that I was reflecting my ray against was parallel to my the light source so the reflected light would bounce back to the lightsource and would go bouncing back and forth...I guess maybe I will either disable my lightsource collider or perhaps use a Layermask for more specificity
Your answer
Follow this Question
Related Questions
Why is the reflected ray the same length as the previous and how to fix it 1 Answer
Raycast Reflection 2D 1 Answer
2D How to reflect a raycast with a line renderer 0 Answers
Having an issue Making a second ray cast with a new angle 1 Answer
Setting positions of points on Line renderer to mouse position does not work 1 Answer