Published on

Laser projectile and explosion on impact like StarWars [unity3d Effect]

  • Evelyn
    Full-time IndieDev.
    I'm Japanese, so please forgive me if my English is strange.

Please share this article if you like!

Hi, welcome to EvelynGameDev!!

In this blog post, we'll work together to display the Particle Effect when a laser bullet collides with an obstacle.

This blog post will help you implement the following.

The content of this article is also explained in detail in a Youtube video.

If you prefer videos to text information, please check out the Youtube video.

To be covered in this article
  • Collision detection
  • Generating Effects

So let's get started!!

Table of Contents

Importing Unity assets

First, import the necessary packages.

You will need to import the following two assets into your 3D project.

Volumetric Lines

Starter Particle Pack

Judging collisions and generating explosion effects

Next, delete the Sample Scene that exists from the beginning.

Then open the scene called Laser Cannon, which is part of Volumetric Lines.

Since the laser firing process has already been implemented in this scene, we will use this one.

Adjust the scene slightly. Set the background of the main camera to black and change the ground to the 3D object Plane.

In addition, open the Starter Particle Pack prefab, and drag and drop the effect VfxBoomSparks into the hierarchy.

Since the effect is currently small, change the following parameters of the Particle System so that you can see it in the GameWindow.

  • Start Speed: 500
  • Start Size: 10 ~ 20

Drag and drop the effect's prefab into the project window to prefabricate it.

Delete the hierarchy VfxBoomSparks.

After that, select any game object and click Add Layer.

Then, enter the layer as follows.

  • User Layer 3: Player
  • User Layer 6: Laser

And then, change the layer of the Cannon game object to the player.

A dialog will appear, and we will change the layers of the child elements as well.

Then select the project's shot prefab and change the layer to Laser.

This will be used later to prevent collision detection when the player and laser collide.

Then, add a Box Collider to the laser and change the shape of the Collider.

This area is the collision detection area for the laser projectile.

It can be slightly larger than the actual shape. You can change it to fit your game characteristics.

In addition, add a RigidBody and disable the User Gravity parameter. This will allow the laser to ignore gravity.

Then click on the ShotBehavior script in the Inspector to make it appear in the project window. Double-click it to launch Visual Studio.

Since only the code to proceed in the direction of travel is currently written, we need to define the method to detect the collision and the process to generate the prefab for the effect on the game screen.

Then change the script as follows


using UnityEngine;
using System.Collections;

public class ShotBehavior : MonoBehaviour {

	public GameObject ExplosionPrefab;

	// Update is called once per frame
	void Update () {
		transform.position += transform.forward * Time.deltaTime * 1000f;

    private void OnCollisionEnter(Collision collision)
		Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);

Once you are back in UnityEditor, click on Edit in the menu bar, and then click on Project Settings.

Then go to the Physics tab and change the Layer Collision Matrix at the bottom.

Uncheck the boxes where the Payer and Laser intersect.

After that, create a new C Sharp file and rename it RemoveExplosion. Double-click it to open it in Visual Studio.

After the explosion effect is generated, we will write the code to destroy the effect after an arbitrary amount of time has passed.

For performance reasons, we should use the object pooling technique, but to avoid unnecessarily lengthening the video, we will simply execute the Destroy method in this video.

Then write a code like the following.


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

public class RemoveExplosion : MonoBehaviour
    public float WaitTime = 1.5f;

    void Start()

    private IEnumerator Remove()
        yield return new WaitForSeconds(WaitTime);

You now have the necessary script. Go back to UnityEditor and navigate to the Example Prefabs folder in Example Scenes.

After selecting the VfxBoomSparks prefab, click the Add Component button from the inspector to add the RemoveExplosion script that you just created.

Next, select the shot prefab and drag and drop the VfxBoomSparks prefab against the Explosion Prefab variable in the Shot Behavior script in the Inspector.

Then, delete the game object called Smoke as it reduces the visibility.

All implementation is now complete. Let's run it and check the behavior.

After working up to this point, you should see the following behavior.

Summary of this article

How was it? If there are any parts you don't understand along the way, please check the video.

Generating effects when objects collide is a commonly used technique, so please apply it to your own game development.

Make your game more exciting by making effects appear when objects collide.

If you like this article, I hope you will follow me on my social network.

Thank you for reading to the end.

Please share this article if you like!