- Home /
Reflect ray of light using mirrors?
I want to make a puzzle where the player need to reflect a ray of light light using rotatable mirrors(2d, Im using sidescroller) , what should I do? I have knowledge at Programming. I just cant come up with an algorithm using the unity objects.
Answer by robertbu · Aug 02, 2014 at 06:09 PM
From the starting position, Raycast() outward. If the Raycast() hits anything, then the RaycastHit.point and the RaycastHit.normal can be used with Vector3.Reflect() to figure out the start and direction of the next Raycast().
Note someone was working on something similar (not precisely this game, but having a ray bounce around) in the last 90 days. His post included a bit of source that might help you, so you might want to poke around on UA to see what you find.
Do i need to put the "Raycast() outward" in a script in the object? what is the exact syntax for this? sorry for asking.
Since I have neither a picture or your current source, I can only guess. Typically you will have an object that is the source for the light ray. Since this is 2D, it will probably be the right side of that object that is the front. So assu$$anonymous$$g the script is on the object that is shining the light, the first raycast might be:
var hit: RaycastHit2D = Physics2D.Raycast(transform.position, transform.right);
For the next hit I'd move up the normal a bit for the position, and you can check the collider to see if it hit anything:
if (hit.collider != null) {
nextPos = hit.point + hit.normal * 0.01;
nextDir = Vector3.Reflect(transform.right, hit.normal);
var nextHit = RaycastHit2D = Physics.Raycast(nextPos, nextDir);
}
If you are going to allow the light to bounce around, you'll have to structure the next hits into a while loop, walking from one hit to the next. Note there is a possibility of a cycle, so you want to put in some logic that ter$$anonymous$$ates the while loop either when a cycle is detected, or when there is so many bounces that there must be a cycle.
Note the code fragments here are just for demonstration of the concepts. If it were me, I'd fold all this logic into a single function that returned a list of hit points with the initial position and direction passed in as parameters.
I wanted to see the bounce work, so I did a quick prototype. Here is the function that does the bouncing:
function FindBouncePoints(startPos : Vector3, startDir : Vector3) {
var pos : Vector3 = startPos;
var dir : Vector3 = startDir;
var count : int = 1;
points.Clear();
var p : Vector3 = startPos;
p.z = -0.01;
points.Add(p);
do {
var hit: RaycastHit2D = Physics2D.Raycast(pos, dir);
if (hit.collider != null) {
p = hit.point;
p.z = -0.01;
points.Add(p);
pos = hit.point + hit.normal * 0.01;
dir = Vector3.Reflect(dir, hit.normal);
count++;
}
} while (count < maxPoints && hit.collider != null);
}
It expect 'points' to be declared at the top of the file:
private var points : List.<Vector3> = new List.<Vector3>();
And here is the result:
sir, im sorry to say this, but Im actually working on 3d, but sidescroller. sorry I put 2D there. :(
It seems there an error: Unexpected Token. in the var nextHit = RaycastHit2D = Physics.Raycast(nextPos, nextDir);
prefer using C#
Answer by Mirecsy23 · Apr 09, 2016 at 06:26 AM
Hi, I made asset that is solved your problem You can get in on AssetStore: http://u3d.as/qBx
Your answer
Follow this Question
Related Questions
Raycasting error? 1 Answer
Up for a challenge? 3 Answers
Ray Light Effect 2 Answers
My raycast is not always in the forward direction. 1 Answer
How is this effect done? 1 Answer