- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Riiich · Mar 25, 2015 at 02:05 PM ·
raycastvector3raycasthitreflection
Stop the lasers from keep bouncing
So I'm using the script found here: Unity Raycast Reflection but I can't get it to stop bouncing. I've tried using a simple:
if(lastHit != "OutWall") {
//the reflection direction is the reflection of the current ray direction flipped at the hit normal
inDirection = 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,inDirection);
//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, inDirection*100, Color.magenta);
//Print the name of the object the cast ray has hit, at the console
Debug.Log("Object name: " + hit.transform.name);
//save the last hit object
lastHit = hit.transform.name;
//if the number of reflections is set to 1
if(nReflections==1)
{
//add a new vertex to the line renderer
lineRenderer.SetVertexCount(++nPoints);
}
//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);
}
But adding my lastHit variable doesn't seem to be working.
Could you guys take a look at his script and determine where I should place my "If OutWall is hit then don't continue bouncing" if statement
Thanks so much for your help!
Comment
Best Answer
Answer by Riiich · Aug 31, 2021 at 11:50 PM
This was correct, I was using a tag instead of the GameObject's name
Your answer
Follow this Question
Related Questions
How to place an object in the direction of a raycast 1 Answer
Click to move script help 1 Answer
Problem With Raycasthit Angle 0 Answers