Nerd For Tech
Published in

Nerd For Tech

Unity Guide

Implementing space shooter game features - Attract power-ups

A quick review of new features added to a space shooter game in Unity

Objective: Implement a way for the player to attract power-ups in a space shooter game with Unity.

In the previous post I implemented a way for some enemies to detect power-ups in my space shooter game with Unity. Now it’s time to implement a way for the player to attract power-ups in the playground to collect them easier.

Power-Up class

In order to make the power-ups follow the player, let’s open the Powerup class and create a new variable that will store a reference to the player’s transform component:

Then, in the Start method, let’s use GameObject.Find method to find the player, and then with the GetComponent method, let’s get the transform component:

Note: We could also use GameObject.FindWithTag to search for the player gameobject.

Also, let’s create a new bool variable that indicates if the power-up is being attracted by the player:

Now, to move the power-up towards the player, let’s create a new method, which will start by calculating the step to move closer to the player. This step will be the result of doubling the speed per second of the power-up:

Then, let’s use the Vector3.MoveTowards to give the power-up position a new value closer to the player’s position:

If you want to know more about the Vector3.MoveTowards method you can visit the Unity docs:

Finally, let’s determine the movement inside the Update method by checking if the power-up is being attracted or not:

Player class

Now, in order to give the player the ability to attract the power-ups in the scene, let’s create a public delegate method and a public static event of it. These will help to change the attraction bool value of the power-ups in scene:

Next, let’s add a condition in the Update method that checks if the player inputs the ‘c’ key, and in that case, if the static event is not null, let’s execute it:

Also, to stop attracting the power-ups, we can use the same event when the player stops pressing the ‘c’ key:

In order to change the attraction let’s create a new method in the Powerup class that gives the current opposite value of the bool to the bool. This way we can make sure that when the method is called the value is set to the opposite:

Then, in the Powerup class, let’s add the method call to the static event from the Player class so that it gets called when the event is executed:

Finally, let’s make sure to remove the method from the static event using the OnDestroy method, which is called before the gameobject with this script attached is destroyed. By doing this, we make sure that the static event doesn’t try to execute a method from a gameobject that doesn’t exist anymore:

And now, if we run the game in Unity, we’ll be able to press the ‘c’ key and attract the powerups in the scene to the player:

And that’s it, we implemented a way for the player to attract power-ups in the scene! :D. I’ll see you in the next post, where I’ll be showing more features added to my space shooter game in Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D



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
Fernando Alcantara Santana

Fernando Alcantara Santana


A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).