Added Gradius’s Option Power Up To My Game

PieceofPhie
4 min readJul 30, 2021

The player now has an assist that will follow and fire a projectile when the player fires. The power up will have a chance to spawn every 7 seconds (for now).

To get this to function, all is needed is a script that’ll handle the following logic and a function to instantiate our player assistants.

Player Assist Script (The Gradius Option)

This is a script that’ll be attached to the player assist prefab

The GameObject Target variable is used to insert any game object for our player assist to follow. The script will check if the target is not null then proceed to execute the following logic.

The initial conditional statement will check the distance between the assist’s current position to the target position. If the distance between the assist and the target is greater than 1.2 unity meters or 0.9 then the assist will begin to move. The further away the assist is, the faster it’ll move towards the Target.

The assist will move towards the target by using Vector2.moveTowards; Taking three parameters, A vector2 for the gameobjects current position, another vector2 for the intended GameObject to move towards, and a float value that’ll set the maximum distance.

This Fire() function can be found in Update() above. Its a simple fire function that is almost identical to the player fire script which allows the assist to fire when the player fires.

previous article related to the player fire function

https://vixian.medium.com/creating-a-cool-down-system-bcfa5e7d2097

Lastly, A public function that will be called outside the script. It’ll reassign the Target variable to whatever is passed through the functions GameObject parameter.

Player Assist Spawning (The Gradius Option Spawning)

A new function will be created within my current player script which will instantiate the player assist and assign it its proper target. Ill break this down into two sections; Declaring the variables and the instantiation function itself.

Declaring The Variables

AssistGameObject is used to store the player assist prefabs to be spawned.

Player_Assist PARetarget allows the scripts to commmunicate to one another

ListOfAssist Stores all instantiated assists so it can be referred to later.

AssistCount keeps track of the last instantiated object so that we may reassign its Target

SpawnAssist() Function

This function will only be called when the player collides with the assist power up. Power ups were covered in a previous article which is found below.

https://vixian.medium.com/day-8-power-ups-and-scrolling-backrounds-32470a6c9952

The SpawnAssist() Function serves two functions. To instantiate the player assist and to assign it a game object to follow depending on its order within the list.

If the assist is first in the list then the assist will follow the player. If the assist is other than first then the assist will follow the game object that was instantiated before it.

Upon calling the function, AssistCount is updated, the assist is instantiated and is stored within a local variable so that it can be referenced. The assist is added to the ListOfAssist and PARetarget is assigned the newly instantiated game object’s player_Assist component.

The player’s assist is now instantiated and is able to be assigned its game object to follow.

The first conditional statement will check if this is the first assist that is spawned. If it is, then it’ll follow the player.

If its not the only assist that is spawned, than it’ll follow the game object that was instantiated previously.

I locate the previous instantiated game object index within the ListOfAssist by subtracting AssistCount by 1. Then it is passed into the Retarget’s function game object parameter.

--

--