Negative Pickup — Freeze Asteroid

Frank Warman
Nerd For Tech
Published in
4 min readMay 25, 2021

As I’ve mentioned in the previous articles, it’s time to start making the Game more difficult for the Player. They have quite a few Powerups to aid them, but no pickup that will affect them negatively… until now!

In this article we’ll be creating a Freeze Asteroid that will slow down the Player almost to a halt for a set amount of time. They better hope no Enemies are quickly approaching!

Freeze! In the name of the [Game’s] Law!

Freeze Asteroid Game Object

First we’ll create the Asteroid pickup that will trigger the fancy new Freeze logic!

Freeze Asteroid preview

I’ve simply re-used the Asteroid sprite and coloured it blue through it’s Sprite Renderer. It looks icy…right?

Blue colour in Sprite Renderer, plus other components

I also added a Circle Collider 2D which has been edited to fit the Asteroid and Is Trigger, and a Rigidbody 2D component with gravity scale set to 0.

I’ve also created a new FreezeAsteroid script to hold our logic for this new negative pickup!

Once the Game Object is created and linked up, don’t forget to add it to the SpawnManager Powerup Array so that it has a chance of being spawned in!

FreezeAsteroid Script

We’ll start with the Global Variables:

And will get our Player and null check at Start():

Next we’ll create the Asteroid’s movement logic. One line for translating down, another for it’s rotation, and an if statement to destroy it once it disappears off-screen:

Feel free to put these in their own separate functions!

Now we’ll also create the OnTrigger logic for when it interacts with a Laser, and the Player.

Be sure to link your _explosionVFX in the Inspector! (You can also ignore the _player.playerLasers.Remove() line… that will make sense in an upcoming article!)

I’ve created a reference to the explosion when Instantiating so that I’m able to then change the scale of the explosion. In this case since my Freeze Asteroid is smaller than my original Asteroid, I’ve scaled down the Explosion VFX by half.

_player.FreezeVFXActivate()

Now our Player can interact with the Freeze Asteroid, but it’s not doing anything at the moment. We have to create the Logic for that in the Player script!

So first we’ll create some variables in the Player script:

Now for the function:

If we are not already _isFrozen, then we’ll cache our current speed in _ogSpeed. Divide our current speed by itself, making it 1. Then start our cooldown.

We’ll also flip our _isFrozen bool to true, turn on our _freezeVFX animation Game Object, turn off the _thrusterVFX Game Object, and start the _freezeVFX animation (which we are about to create).

Our Cooldown Routine

Our cooldown routine will reverse everything we did in the FreezeVFXActivate() function after our cooldown timer expires.

Freeze VFX Animation

To create our Freeze VFX we’ll be using another instance of the Asteroid Sprite and animating it using it’s Scale and Alpha transparency.

We’ll create a new Game Object using the Asteroid Sprite, and attach it as a child to our Player:

Then create a new animation clip with Player_Freeze_VFX selected to create the Animator controller and animation:

Freeze VFX animation effect

I’ve just played with the scale and alpha transparency to achieve this effect. The sample rate was also tweaked to get the timing to feel right.

We’ll also create an Empty State in the Animator Controller so that we can trigger the animation to happen:

Empty State with transitions.

Our transitions are controlled by creating a bool parameter called “Player_Frozen”, and using it as a condition for each transition:

*When creating the animation, make sure that it is not set to loop!

Once the Animation has been created, the Player script has been given logic, and the Negative Pickup has been created and thrown into the Spawn Manager Powerup array, you’re Player is now in for some new danger!

In the next article we’ll be adding to that danger by creating a new Enemy that has nothing to lose!

--

--

Frank Warman
Nerd For Tech

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles