Day 52: Pick-Up Collect

Alex Twyman
2 min readMar 22, 2023

--

Let’s create a way for the Player to collect the Pick-Ups easier. We will use an Input to control when we want to move the Pick-Ups toward the Player. We can do the same thing we did for the Enemy that tries to ram into the Player.

Code

Open the PickUp script. We will need two new variables and a reference to the Player.

In the Start function, let’s find the Player GameObject and then do a null check.

In the Update function, we will constantly track the distance between the Player and the PickUp.

Now we look for an input on the keyboard. In this case, we are looking for when the Player holds down the C key. If so, the PickUp will move towards the Player. If not, the PickUp will just move down.

Now for the MoveTowardsPlayer method. We are using the exact same method we did for the Enemy.

If you want to see how we did this for the enemy. You can check out that article here:

Let’s see our code in action!

--

--

Alex Twyman

A Unity Developer on a journey to create amazing games.