Add an aiming line in Unity 2D

Daniel Kirwan
2 min readOct 19, 2021

--

Objective: Show the player the aiming line

If you followed the previous article you know how I created the rotation of the turret and changed the cursor to show the crosshair.

I am using the same script as from the previous article called AimTurret. First I created some variable that I will need to complete the line.

Once the variables have been made save the script and go back into Unity. First, make sure that a gameobject has a line renderer component that is needed to draw the line on screen. I have mine on the turret.

I then drag in the line renderer into the empty space in the AimTurret script. Now go back into the script and now it is time to create the functionality of the line. Another, note before going back to scripting, you can change the default line by adding in a material to the line renderer.

Now, back into the script.

First we start by setting the mousePos to the current position of the mouse. Then we get the direction by making a simple subtraction and then normalising the direction to have a value of 1.
Then enable the line renderer.
I then set the start and end positions for the line renderer. First I set the start position with lr.setposition with the start position and then make the end position somewhere between the maximum line length of 5 using a clamp. The final part is to set the final position of the line with the end position.

And that is it for creating an aiming line in Unity2D.

--

--