How To Make A Laser Beam In Unity

Unity 3D Tutorial Laser Beam Turrets using Particle System PREFAB

Bullet points should use

    and

    Read More
  • tags.

    Unity is a powerful game engine that can create stunning 3D visuals, immersive experiences, and innovative gameplay. It is also capable of making laser beams, which can be used to add a unique element to your game. In this article, we will show you how to make a laser beam in Unity.

    The first step to making a laser beam in Unity is to create an empty game object. This game object will be the parent of the laser beam. To create an empty game object, right-click on the Hierarchy window and select “Create Empty”. Then, select the empty game object and name it “Laser”.

    The next step is to create a material for the laser beam. To do this, select “Create” from the Assets menu and choose “Material”. Then, name the material “LaserMaterial”. You can then assign the material to the laser by selecting the laser game object in the Hierarchy window and then dragging the material from the Project window onto the Laser game object.

    Next, create a cube and name it “LaserCube”. This cube will be used to create the laser beam. Select the cube and in the Inspector window, set the Scale to (0.1, 0.1, 1) and the Position to (0.5, 0, 0). This will make the cube the right size and position for the laser beam.

    Now, create a script for the Laser game object. This script will control the laser beam. To create a script, right-click on the Project window and select “Create > C# Script”. Name the script “LaserController”. Then, select the Laser game object and drag the script onto the Inspector window.

    The next step is to create a prefab for the laser beam. To do this, select the LaserCube game object in the Hierarchy window and drag it into the Project window. This will create a prefab of the cube. Then, drag the prefab onto the Laser game object in the Hierarchy window.

    Now, open the LaserController script and add the following code. This code will control the laser beam. It will move the laser beam from left to right and then back again.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class LaserController : MonoBehaviour {
     
     public GameObject laserPrefab;
     private GameObject currentLaser;
     private float laserSpeed = 8f;
     private float leftLimit = -8f;
     private float rightLimit = 8f;
     
     void Update () {
     
     if (currentLaser == null)
     {
     currentLaser = Instantiate(laserPrefab) as GameObject;
     currentLaser.transform.position = transform.position;
     currentLaser.transform.parent = transform;
     }
     
     Vector3 pos = currentLaser.transform.position;
     pos.x += laserSpeed * Time.deltaTime;
     
     if (pos.x < leftLimit)
     {
     laserSpeed = Mathf.Abs(laserSpeed);
     }
     else if (pos.x > rightLimit)
     {
     laserSpeed = -Mathf.Abs(laserSpeed);
     }
     
     currentLaser.transform.position = pos;
     }
     
     }

    The final step is to create a shader for the laser beam. To do this, right-click on the Project window and select “Create > Shader”. Name the shader “LaserShader”. Then, open the shader and add the following code.

    Shader "LaserShader" {
     
     Properties {
     _Color ("Color", Color) = (1,1,1,1)
     _MainTex ("Albedo (RGB)", 2D) ="white" {}
     }
     
     SubShader {
     Tags { "RenderType"="Opaque" }
     LOD 200
     
     CGPROGRAM
     #pragma surface surf Lambert
     
     sampler2D _MainTex;
     fixed4 _Color;
     
     struct Input {
     float2 uv_MainTex;
     };
     
     void surf (Input IN, inout SurfaceOutput o) {
     fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
     o.Albedo = c.rgb;
     o.Alpha = c.a;
     }
     ENDCG
     }
     
     FallBack "Diffuse"
     
     }

    Finally, assign the shader to the LaserCube prefab. To do this, select the LaserCube prefab in the Project window and in the Inspector window, select the Shader dropdown and choose the LaserShader. And that’s it! You have now successfully created a laser beam in Unity.

    Making a laser beam in Unity is a great way to add a unique element to your game. With just a few simple steps, you can create a stunning laser beam that will look great in your game. So, if you’re looking for a way to make your game stand out, give making a laser beam in Unity a try!