/LaserReflections

Primary LanguageC#MIT LicenseMIT

The LaserBeam class is a Unity script that simulates a laser beam that can reflect off mirrors and activate laser receivers. Here's a breakdown of the code:

Member Variables:

  • maxDistance: The maximum distance the laser can travel in one step.
  • lineRenderer: A component that renders the laser beam as a line.
  • points: A list of points that define the path of the laser beam.

Start Method:

  • Initializes the lineRenderer by getting the LineRenderer component attached to the same GameObject.

LateUpdate Method:

Note that we use LateUpdate because we want to make sure that the material of the Receiver is applied as a last step in the frame.

  • Clears the points list and adds the current position of the GameObject as the starting point.
  • Sets the initial direction of the laser beam to be upwards.

Uses a loop to simulate the laser beam:

  • Casts a ray in the current direction to check for collisions.

If it hits something:

  • Adds the hit point to points.
  • If it hits a mirror, calculates the reflection direction and continues.
  • If it hits a laser receiver, activates the receiver and stops the laser.
  • If it hits something else, stops the laser.
  • If it doesn't hit anything, adds a point at the maximum distance and stops the laser.
  • Updates the LineRenderer with the points in points.

Pseudocode:

class LaserBeam:

member variables:
maxDistance = 5
lineRenderer
points = []

    method Start:
        lineRenderer = get LineRenderer component

    method LateUpdate:
        clear points
        add current position to points
        direction = up

        while foundHit is true:
            currentPosition = last point in points
            if raycast from currentPosition in direction hits something:
                add hit point to points
                if hit object is a mirror:
                    calculate reflection direction
                else if hit object is a laser receiver:
                    activate receiver
                    foundHit = false
                else:
                    foundHit = false
            else:
                add point at max distance in direction to points
                foundHit = false

        update LineRenderer with points