Creating a negative pickup in Unity

The exercise here demonstrates a way of introducing a powerup that stops the player from firing for 5 seconds.

The spawn manager takes care of spawning powerup prefabs and the powerup script gives these prefabs speed and collision capabilities. Please make sure that there is a similar configuration at your end to follow along.

First, a prefab has to be created.

  • In order to do it, have a powerup sprite in the project, attach relative collider and rigidbody components to the sprite by bringing the sprite into the project.
  • Please keep the “Is Trigger” option ticket so that it behaves like a powerup and the “Gravity” scale to 0 so that the powerup script can provide a controlled movement.
  • Then, do not forget to add the powerup script component with its’ required parameters.
Unity

Now that the prefab is in place, the next part is to halt the laser firing of the player when this prefab gets collected.

The challenge here is that the fire method happens to be in the update( ) method which gets called every frame. So, even if there is another method that intends to do something else with the fire method, it would not come into effect since the update would override its execution.

Therefore, there has to be a condition check that can help to halt the firepower despite getting calls from the update method.

Let us implement it.

  • Declare a bool variable in the player script.
private bool LaserDamagerCollected = false;
  • Go to the fire method and add a condition check with this variable.
C#
  • Create a method to change this variable value to true and call the method from the powerup script.
C#
C#

Now, we need something that stops the execution until a condition is met. Coroutine !!

  • Before making it, a good part is to have a graphical way that makes the player aware of the unfortunate collection.
  • So, create a text element in the canvas and set it as per choice with an empty text. After that, get a reference to it in the player script.
Unity
[SerializeField]
private Text _laserShockText;
  • Create a coroutine that counts for 5 seconds before turning the if condition to false again. Meanwhile, the text shall keep displaying a message with a countdown.
C#
  • Lastly, call this coroutine in the method that was created prior.
C#

That should be it.

Here is the final result :)

Thank you very much

Work in Progress.