Project 1: Space Shooter

New type of fire powerup for the player

How to make a scatter shot in Unity

Eero Saarinen

--

The scatter shot in action.

Time for a new way of shooting powerup for the player, called scatter shot. In essence this fire mode mimics a shotgun shot, but with lasers.

Now I know that the powerup looks like a regular Unity 3D object called sphere and it truly is that. I did use some time, like an hour or so trying to find a good sprite or 2D model for the powerup, but eventually decided to go ahead with the actual implementation instead of looking for fancier graphics for now. Otherwise the sphere holds the normal stuff, i.e. RigidBody2D, CircleCollider2D and the Powerup script.

The script for the scatter shot.
The scatter shot script.

In any case the basis for my scatter shot prefab is an empty game object with a script attached to it. Everything is done inside the Start method since the implementation is quite simple. All the script does is it instantiates ten laser prefabs with a random rotation around the z-axis. The rotation is handled with the Quaternion and its method Euler. Once all the lasers have been instantiated the scatter shot game object is destroyed so that it does not clutter the Hierarchy.

The activation and the deactivation methods inside the player script for the scatter shot.

Then the last thing to do is to add a public method called ScatterShotActive inside the player script, which activates the scatter shot and starts a power down coroutine called ScatterShotPowerDownRoutine to deactivate the scatter shot after five seconds. Not forgetting to add the firing mechanism inside the FireLaser method.

The script for firing the scatter shot inside the FireLaser method in the player script.
How to fire the scatter shot once it has been activated.

--

--