Making Lasers & Similar Attached Projectiles

v1.0

Sometimes a projectile doesn't move. Lasers, flamethrowers, and any other kind of projectile that is intended to stay in one spot, or attached to a Transform, can be created! This guide will walk through the setup for a laser projectile which stays attached to the Spawn Point.

We will be using the Magic Arsenal by Archanor VFX on the Asset Store for this. The "Acane Beam" is the beam itself, using a LineRenderer. The other two parts will be attached to the muzzle position and the "hit point" -- the place where the beam collides with another object.

1. Add a Projectile Compnent to the Beam

Use the "Damage Over Time" Projectile Data, and the "Laser" Spawn Behavior.

The DamageOverTime class overrides how the damage is calculated, to multiply it by Time.deltaTime.

protected override float CalculateDamage() => damage * Time.deltaTime;

Laser Spawn Behavior is set up for a laser, including blocking multiple shots. Check the object for specifics.

2. Add a Raycast Shooter Component

The RaycastShooter class is provided in Projectile Factory, and will fire a raycast forward, and pass hits to handlers on other objects in the prefab. Be sure to assign the Raycast Origin value.

3. Add a Line Renderer Length Component

The LineRendererLength class handles the setting the length of the LineRenderer to the distance of any collision, or the max distance provided. This compoennt implements IHandleRaycasts, so the Raycast Shooter will pass the Hit value to this class.

4. Set up the Projectile

Add a Spawn Behavior Modification

Add a Parent to Spawn Point Spawn Behavior Modification.

There are no exposed parameters here -- this does one thing, and one thing only. After spawning, the Projectile will be parented to the Spawn Point transform.

Add Behaviors

We will add two Behaviors to this Projectile. First, Destroy on Spawner Stop will destroy (or pool) the projectile when the spawner stops -- in the demo, this is when the "shoot" button is released.

Collision at Line Render End will get the Projectile to check for a collision at the end of the LineRenderer, with the radius selected.

5. Set up the Muzzle & Impact Particles

The Muzzle and Impact particles aren't part of the Projectile directly. Instead, they'll be child objects of the Projectile, and will be handled by other scripts.

Position the Objects

The Muzzle object should be positioned at local 0, 0, 0 -- basically the same spot as the LineRenderer origin, which will be the Spawn Point.

Add a SetPositionFromRaycast class to the Impact object. Be sure to assign the "Plumbing" values. This implements IHandleRaycasts, so the RaycastShooter will handle passing data to this script, which is how it knows where to set the position.

Optional: Add Force

You can also add an AddForce component to the Impact object. This will add physics force at that position, helping the game come alive.

6. Set the Layer of the Projectile

Don't forget to change the layer of your projectile! And then save this as a Prefab.

Done!

That's it! Bring this onto a Projectile Spawner the same as any other Projectile, and it'll function exactly the same, but with its own unique logic and behavior.

Last updated