Automatically Attract Power-Ups to the Player

Objective: Attract collectibles, power-ups or any object of your choosing toward the player.Programming the “Magnet”This code will be added to my Modular Power-Up Script where all the Power-Ups’ behavior is handled. Ideally, the placement of this code …


This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila

Objective: Attract collectibles, power-ups or any object of your choosing toward the player.

Programming the “Magnet”

This code will be added to my Modular Power-Up Script where all the Power-Ups’ behavior is handled. Ideally, the placement of this code needs to be in a script that is attached to the object you want to attract to the player since we will be accessing its transform capacities to get the desired result.

  1. Create a global variable for the speed at which the object will move toward the player. I added a SerializeField so that I can tweak the value of the speed from the Inspector should I find it necessary.

2. Create a global variable to reference the Player object. This is done so you are able to access the Player’s transform later on.

3. Find the Player object in the Start function.

Note: As a best practice, you should always null check when finding GameObjects and Components.

Now that you have set up all the necessary variables, it’s time to learn about Vector3.Lerp. Simply put, this allows us to interpolate the points between point A and Point B.

What does that mean for us?

We can use this so that object A (the collectible), moves toward Object B(the player) since it will be moving across any set of points between them no matter in what position they are.

4. Create a method to handle the attraction logic. Here, the collectible object’s position is being changed through the usage of Vector3.Lerp. The syntax goes as follows: Vector3.Lerp(StartingPoint,EndPoint,Speed).

5. Call the method from the update or where you see fit. Personally, I set up the method to be activated through Player Input. Although be aware that if you use Input to achieve this, you must not use GetKey instead of GetKeyDown. GetKey will allow the movement to flow continuously rather than make the object move one unit every time the key is pressed. This is because the Input in GetKey is true as long you hold the key down, so the logic within it will run every frame.

The Result:

In the next article, I will go over how to add a hazard collectible!


Automatically Attract Power-Ups to the Player was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila


Print Share Comment Cite Upload Translate Updates
APA

Dennisse Pagán Dávila | Sciencx (2021-07-30T17:06:01+00:00) Automatically Attract Power-Ups to the Player. Retrieved from https://www.scien.cx/2021/07/30/automatically-attract-power-ups-to-the-player/

MLA
" » Automatically Attract Power-Ups to the Player." Dennisse Pagán Dávila | Sciencx - Friday July 30, 2021, https://www.scien.cx/2021/07/30/automatically-attract-power-ups-to-the-player/
HARVARD
Dennisse Pagán Dávila | Sciencx Friday July 30, 2021 » Automatically Attract Power-Ups to the Player., viewed ,<https://www.scien.cx/2021/07/30/automatically-attract-power-ups-to-the-player/>
VANCOUVER
Dennisse Pagán Dávila | Sciencx - » Automatically Attract Power-Ups to the Player. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/30/automatically-attract-power-ups-to-the-player/
CHICAGO
" » Automatically Attract Power-Ups to the Player." Dennisse Pagán Dávila | Sciencx - Accessed . https://www.scien.cx/2021/07/30/automatically-attract-power-ups-to-the-player/
IEEE
" » Automatically Attract Power-Ups to the Player." Dennisse Pagán Dávila | Sciencx [Online]. Available: https://www.scien.cx/2021/07/30/automatically-attract-power-ups-to-the-player/. [Accessed: ]
rf:citation
» Automatically Attract Power-Ups to the Player | Dennisse Pagán Dávila | Sciencx | https://www.scien.cx/2021/07/30/automatically-attract-power-ups-to-the-player/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.