Learn to code by making a 2D space shooter game.
Objective: Pickup Collect, When the ‘C’ key is pressed by the Player, Pickups
quickly move to the player.
Before doing that, create a new script called Pickup Collect to make each class do its responsible since cluttering everything in the player script is not ideal, this script goes to the player game object.
In the first step, we need two variables and game object and the bool variable.
In the next step, create a normal method called Collect.
In the third step, we need to find the object, we do this by using GameObject.FindGameObjectWithTag.
In the fourth step select all powerups and add tag powerup.
best practice when accessing components Is to null check.
In the next step, we need to check for the distance between the player and powerup, we use vector2.Distance and compare with the unit if it's less than that number.
In the next step, we need to move the powerup to the player, we do this by setting the powerup position and then using the MoveToward method to move the powerup to the target position with a specified speed.
In the last step, we need to allow the player if he presses the c key to allow powerup to move to the Player quickly.
Note: if we press the C key like this the collect method will be called more than once to fix this we need to set the bool variable here to true and otherwise to false.
Let's see the final result.
Thank you for reading up to here, see you in the next article.