Update: Doing some more experimenting... it's not the early break that's causing the problem in the slightest. It does the same thing if I set the reflections to more than 2 and have no if or break in there.
So, I need to rethink this whole thing from scratch and try to find a better tutorial, I guess.
I'm working on a game for a game jam (I asked, asking for help is permitted :-)) involving lasers that reflect off of mirrors that are adjusted by the player to make the laser hit a target. Thanks to some tutorials, I managed to get the lasers to reflect, but they reflect off of everything and won't stop unless I manually set a number of reflections to stop at.
I've tried adding an if statement to break the reflection loop if it doesn't hit a mirror, as you'll see in the code, but it just results in a laser going off on a weird angle after hitting the non-mirror target.
I should add that I'm very new at this. I'm basically figuring things out as I go for this game jam. So, I know it's probably a simple mistake, or maybe I followed the completely wrong tutorial, but I just can't see it.
Can anybody please point me to the mistake?
void Update()
{
//clamp the number of reflections between 1 and int capacity
nReflections = Mathf.Clamp(nReflections, 1, nReflections);
//cast a new ray forward, from the current attached game object position
ray = new Ray(goTransform.position, goTransform.forward);
//represent the ray using a line that can only be viewed at the scene tab
Debug.DrawRay(goTransform.position, goTransform.forward * 100, Color.magenta);
//set the number of points to be the same as the number of reflections
numPoints = nReflections;
//make the lineRenderer have that number of points
lineRenderer.positionCount = numPoints;
//Set the first point of the line at the current attached game object position
lineRenderer.SetPosition(0, goTransform.position);
for (int i = 0; i <= nReflections; i++)
{
//If the ray hasn't reflected yet
if (i == 0)
{
//Check if the ray has hit something
if (Physics.Raycast(ray.origin, ray.direction, out hit, 100))//cast the ray 100 units at the specified direction
{
//the reflection direction is the reflection of the current ray direction flipped at the hit normal
direction = Vector3.Reflect(ray.direction, hit.normal);
//cast the reflected ray, using the hit point as the origin and the reflected direction as the direction
ray = new Ray(hit.point, direction);
//Draw the normal - can only be seen at the Scene tab, for debugging purposes
Debug.DrawRay(hit.point, hit.normal * 3, Color.blue);
//represent the ray using a line that can only be viewed at the scene tab
Debug.DrawRay(hit.point, direction * 100, Color.magenta);
//Print the name of the object the cast ray has hit, at the console
//Debug.Log("Object name: " + hit.transform.name);
//if the number of reflections is set to 1
if (nReflections == 1)
{
//add a new vertex to the line renderer
lineRenderer.positionCount = ++numPoints;
}
//set the position of the next vertex at the line renderer to be the same as the hit point
lineRenderer.SetPosition(i + 1, hit.point);
}
}
else // the ray has reflected at least once
{
//Check if the ray has hit something
if (Physics.Raycast(ray.origin, ray.direction, out hit, 100))//cast the ray 100 units at the specified direction
{
//the refletion direction is the reflection of the ray's direction at the hit normal
direction = Vector3.Reflect(direction, hit.normal);
//cast the reflected ray, using the hit point as the origin and the reflected direction as the direction
ray = new Ray(hit.point, direction);
//Draw the normal - can only be seen at the Scene tab, for debugging purposes
Debug.DrawRay(hit.point, hit.normal * 3, Color.blue);
//represent the ray using a line that can only be viewed at the scene tab
Debug.DrawRay(hit.point, direction * 100, Color.magenta);
//Print the name of the object the cast ray has hit, at the console
// Debug.Log("Object name: " + hit.transform.name);
//add a new vertex to the line renderer
lineRenderer.positionCount = ++numPoints;
//set the position of the next vertex at the line renderer to be the same as the hit point
lineRenderer.SetPosition(i + 1, hit.point);
}
}
if (hit.collider.tag != "Mirror")
{
break;
}
}
}
Screenshot from my prototype of the extra reflection. It should not reflect off of that cube, and especially not at that weird angle (the foreground object is my primitive laser model, shooting off to the right):