Implementing a special fire up in Unity

Samarth Dhroov
3 min readJun 2, 2021

--

The exercise here demonstrates a quick way to introduce a secondary powerup that runs through the game rarely compared to the other powerups.

Here is what you would need before going into the process.

  • A well functioning spawning mechanism
  • A powerup steering

Without these two, the next steps won’t make much sense or difference. But if both of these needs are in place, please go ahead and alter the steps for your project as needed.

The idea here is to create a wiper that gets instantiated just a little above the player when a powerup is collected and, basically wipes of incoming enemy prefabs and associated laser shots.

the normal pseudo code is as followed.

  • Here is the laser shot in green color that shall behave as the special laser & a powerup prefab that shall instantiate the laser.
Unity
  • Enable the rigidbody2d and colliders with needed configs such as gravity scale to 0, box colliders in appropriate shape and, “IsTrigger” tick marked.
  • Attach the powerup script component to the powerup prefab and provide needed details.
Unity
  • Open the spawn manager script and introduce a new serialized field variable that will hold the laser prefab.
[SerializeField]
private GameObject _greenWiperPower;
  • Drag the laser prefab in spawn manager’s new variable in Unity.
Unity
  • Since the requirement is to spawn it at much slower intervals than other prefabs, a new coroutine shall be written in the script.
C#
  • To instantiate the laser prefab, the player script needs a new method that gets called from the powerup script.
  • To implement this, we first need to assign the prefab through a serialized field to the player script.
//secondary player shot variable
[SerializeField]
private GameObject _secondLaserShot;
  • The trick here is to rotate it to 90 degrees so that it lays horizontal to the player compared to its traditional laser.
public void GreenWiper()
{
Instantiate(_secondLaserShot, transform.position + new Vector3(0.0f,1.5f,0.0f),Quaternion.AngleAxis(90.0f, Vector3.forward));
}

So far, both the laser shot and powerup have been instantiated at a needed speed and location. From here, the laser itself should take care of the moving and scaling part. To do it, we need to attach a script to the prefab.

Unity

The movement script shall have a coroutine that keeps expanding the scale of the laser prefab until it goes out of the screen.

C#
  • Lastly, there is a need to check for collisions that can be detected on the enemy and its’ laser prefabs.
IN THE ENEMY SCRIPTif(other.tag == "GreenLaser")
{
player.AddScore(10);
_TriggerAnimation();
_explosionAudio.Play();
}IN THE LASER SCRIPTif(collision.tag == "GreenLaser")
{
Destroy(this.gameObject);
}

Here is the final result :)

Thank you very much

--

--

No responses yet