Reflecting Rays in 2D game

July 27, 2019



Many games are available in the store that is based on the principle of reflection. They use simple, yet intuitive designs to showcase reflecting rays in the game.
In this article, I will be explaining how we can also create rays reflection in a 2D game in Unity.
Before getting started, have a glimpse of what you are going to make in this article.

Beginning with the Setup

The article explains reflection of rays in a 2D game so make sure your camera is in Orthographic projection mode before proceeding.

Creating the Rays

Create an empty gameobject and attach a Line Renderer to it. Now adjust the properties of the Line Renderer as per your need. This line renderer will be used to draw original rays and their reflection. You may set the width/material of the line renderer.
After making some adjustments, the line drawn by line renderer looks like the image below.

Setting up the Rays Reflection

Now we have a gameobject with a line renderer on it. Now we will use this gameobject to make reflections.
Create a new script named ReflectRays.cs and replace the content of the script with the following code.

Now attach the script to the gameobject containing the line renderer and set the Start Point to (-10, 10) and Direction to (0, 0). This means our ray will originate from coordinate (-10, 10) towards the Direction coordinate.
Now run the game, you will see a ray crossing through the scene something like this.

Creating Reflections

To create rays reflections, simply create a Sprite and attach a 2D collider to it and position that sprite to cut the ray. You will see the rays reflecting as soon as the sprite coincides the rays.


You Might Also Like

0 comments

Follow by Email