- Home /
bouncing light using mirrors,raycast
I want to make a puzzle where the player need to rotate mirrors to make the ray of light reach a specific object, here is a simple image of what was I'm doing,it has a script just to rotate the mirror , I don't understand the Raycast. Please help
Here is a link to @deathmoony81's previous, similar question.
http://answers.unity3d.com/questions/762234/reflect-ray-of-light-using-mirrors.html
Contrary to my answer, apparently he is looking for a single bounce in 3D (not 2D), and any example code needs to be in C#.
I want the player to see the light. because my game is a educational, about physics optics.
Now I understand the Vector3.reflect , but it is used only for computation, No visual .I want to render it.
Your problem is actually a collection of problems, all of which have been covered on UA multiple times. Here is a list of potential problems:
How to do a RayCast()
How to detect kind/nature of the object the Raycast hits
How to find the reflected angle and position for the second Raycast
How do draw lines
How to orient lines to the positions and directions of the Raycast.
There is no right or easy way to draw lines in Unity. Here is a partial list of solutions:
LineRenderer
Vectrosity (add-on line drawing package from Asset store)
Using Quads with line textures billboarded to the camera
Using 3D object like a Cylinder
GL class
For just a single bounce, I'd suggest you consider drawing the line with a couple of Quads for lines and a Quad for the point of contact. The code to scale and align a Quad between two points has been posted to UA several times. And by using Quads, the potential exists to animate the line to give things more life. If you are going to do multiple bounces, I'd recommend investing in Vectrosity, though you could try the LineRenderer to get you started.
Note you can use Debug.DrawLine() with Gizmos turned on in the game window to see lines during development of the bounce code if you've not figured out a line drawing solution.