Phase II: Pickup Collect

Brian Branch
3 min readJun 8, 2021

--

Objective: When the player presses the ‘C’ key, pickups quickly move to the player.

In the heat of battle, it can be dangerous for the player to fly across the screen to grab a pickup, especially when it can be very near enemies. So I’m going to give the player the ability to “tractor” the pickups toward them.

There are two options I’m going to explore. The first is for pickups to move towards the player only while the “C” key is held down and then resume their original direction if released. The other is when the ‘C” key is pressed once all the pickups on screen fly to the player until they are picked up.

Both ways will have some things in common, so I’ll go over that first.

I will need a reference to the player, so I will create a variable for that, then assign the variable to the Player gameobject in the Start() method.

GameObject variable for the player and getting handle in Start().

Next, I’m going to create a method to make the pickup move toward the player. I’ll name the method BeingCollected().

The BeingCollected() method to move pickup toward player.

Now in the Update() method where the pickup movement is calculated, I’ll add an if statement to check if the “C” key is being held down. If so, it will call the BeingCollected() method, and otherwise, it will just move downwards as usual.

This checks if the “C” key is being held down.

This is all that is needed for the first way I mentioned at the beginning. The only things needed for the second functionality are creating a new bool to see if the pickup is being collected and a little change with the if statements in the Update() method.

First, I add a new bool named _isBeingCollected.

Then I modify the check for input to use GetKeyDown instead of Getkey, and if it returns true, I change the bool _isBeingCollected to true. Then in another if statement, I call the BeingCollected() method when _isBeingCollected is true, else the pickup just moves normally.

Below is the player collecting pickups with the second method.

That how to implement two ways of having the player collected pickups.

I hope you found this interesting, and as always, I wish you well on your coding journey.

--

--