I need a way to have a game object that emits a laser. I want it to also reflect off of "mirror" objects and eventually hit a target that sets a bool to true. Think the lasers in Portal 2. How can this be achieved?
2 Answers
2Here's what I'd try first (this assumes that the 'mirror' objects have colliders attached to them):
Start by raycasting along the laser's initial path. If it hits a 'mirror' object (as determined by e.g. the object's tag), reflect the laser's direction vector across the collision normal (that is, 'bounce' the laser), and reset the laser's origin to the collision point. Then repeat. The process terminates when the laser hits a non-mirror surface, or hits the target surface.
Note that you might need to tweak things a bit to ensure that the laser doesn't intersect a mirror surface that's it's just bounced off of. (One way to address this would be to move the laser's new origin along the normal a bit to ensure that it's 'outside' of the mirror object.)
For the visual aspect of it, you could use a procedural mesh, a line renderer, etc.
Do a Raycast, get the surface normal and reflect at the point of incidence.