Power Ups And Scrolling Backgrounds!

Today Ive added the ability to shoot triple shots by touching a triple shot power up! The powerup will last for five seconds until another triple shot power up is picked up. The power up’s duration can be extended if the player picks up another power up while being powered up.

As well as a continuous scrolling background to create the illusion of traveling in space.

Triple Shot Prefab

I begun the process of creating the triple shot by creating a triple shot prefab. The Prefab will be three of our existing laser prefabs parented by a empty game object called,’ Triple Shot”.

I make sure the triple shot is centered with our player’s ship along with its parent. If the parent is not aligned, then the lasers will be off alignment when instantiated.

Instantiating Triple Shot

In this function, I begin by declaring the followiing variables;

A game object variable for our triple shot prefab so that our instantiate can refer to,

A float variable called ,’TripleShotTime’ to handle the power up’s duration,

and a boolean called,’IsTripleShotActive’ to check if have triple shot.

Our code is no different from the regular laser. all it is doing is just instantiating the tripleshotprefab when isTripleShotActive equals true.

Triple Shot Power Up

The boolean should only be set to true if the player has collided with the Power Up game object.

we create an empty game object and name it,’TripleShotPowerUp’. This game object needs to have a rigidbody and a collider to detect collisions with our player. Our game object will also have its own script to handle movement, collisions, and destroy logic.

I’ll create a new script called ,’Powerup’ and declare one float variable to control speed. The two things that will be executed in update are the object translating downward per frame and a calling a function named ,’ Boundaries’.

The functions sole purpose is to check if the game object has gone beyond -5.8 in the Y axis. If the statement is true, then the object will be deleted.

going beyond -5.8 in the Y axis is going out of the main cameras view. The opportunity to grab this power up is lost when the player fails to grab it before heading out of the camera’s sight.

lastly we insert OnTriggerEnter2D. This checks if the game object has collided with our player. If the collided object’s tag equals player then the if statement will get the player component and call the TripleShotActive function. The function will set the IsTripleshotActive boolean within the ,’Player’ script. The If statement will end with the game object being destroyed.

There isn't a TripleShotActive function yet so lets go over that.


Inside our player script, we have a TripleShotActive function that handles the triple shot boolean and its duration.

Every time the player picks up a triple shot power up, it will begin a timer. I compare the games current uptime to the games current up time plus five seconds.

Ex: lets say the game has been running for 30 seconds. When the function is called, the equation will look like this: 30 seconds > 35 seconds. The number 30 will increase since 30 is our current game time. So the statement will eventually become true over time.

However if the player is already powered up and grabs another power up, we add extend the duration five more seconds.

Once the statement becomes true, then the player will no longer have triple shot. The IsTripleShotActive boolean is set to false.

Modifying Fire()

We go back to our ,’Fire’ function within our ,’Player’ script and modify the if statement. We check if the IsTripleShotActive boolean is true and if Time.time is less than Tripleshot time. If both are true, then the player will instantiate our triple shot prefab. If either is false, then our triple shot boolean is set to false and our laser prefab will instantiate instead.

Spawning Power Ups

Next we go into our ,’Spawn Manager’ script to create a co routine and game object variable for our power up. This co routine will behave exactly like our enemy co routine and spawn the power up almost the same way. The only difference is that we pass a random value between 3 and 8 into our WaitforSeconds yield instruction. Making it spawn more randomly than our enemies.

As long as the player is alive, our co routine will instantiate whatever is passed into our ,‘Power up’ game object variable and wait between 3–8 seconds before executing again.

Scrolling Back Grounds

(backgrounds separated for demonstration purposes)

The background will have a script that will translate the game object(background) by a float variable called,’scrollspeed’ per second. Once the background gets out sight, the background will transform its position above the main camera’s view and right above the second background.

The second background will already have seamlessly taken its place and the first background will begin to encroach into the players view. Vice versa to the second background.

I create a script called,’ScrollingBackground’ and attach it on both backgrounds. The script translates the background downward and will transform the position up top.

we have translation occurring in update() aswell as a function being called named ,’Bounds()’.

Bounds checks if the gameobject’s Y position is less than -9.30f (out of view of the camera). If it is, then we’ll teleport the background behind the other background.

I first Position the first background(BG1) to where the bottom is slightly out of the bottom of the main camera. I also leave some of the background out on top so that there is no gap when the second ground comes in.

I select my game object and hold CRTL+V to snap the second background to the first.

the second background(BG2) should be aligned perfectly so we can accurately capture the Y axis here. We get the Y axis of the second background (the one that is currently selected) so we can know when to transform this object behind the First background(The one thats currently in the camera’s view).

The Y axis is -9.22.

we plug -9,22 into our if statement within bounds since that is considered outside our camera’s view.

next we snap BG2 on top of BG1 to get the Y position again. This will let us know where BG2 should transform to.

its Y is 12.38

we plug 12.38 into the vector3’s Y axis.

return BG2 back to the bottom of BG1. Adjust BOTH scrollspeed variable on on both backgrounds in the inspector. Hit Play.

mine ended up having a small gap in between.

So i went back into the bounds function and tweaked the number

nice and smooth, but there's a seem. That’s more of the art issue itself than a programing issue. regardless, it still looks great and makes the game feel more whole.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vixian Phimmasone

Vixian Phimmasone

Unity Developer Based out of California