This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin

Wouldn’t it be great to be able to move objects with your mind? In this article, my objective is to have the Player move Powerups quickly towards itself when the C key is pressed.
I want the powerup to retain it’s normal movement while the player is not summoning it. Having the powerup change movement from it’s own script when a specific key is pressed is fairly straight forward. Having the powerup change movement when the player presses the key needs a few more steps, so let’s get started.
Firstly, I create a variable at the top of the player class to store a boolean value. This will change from false to true when the payer starts pulling powerups towards it, and reset back to false when the player stops pulling.

Next up is a method (PullPowerupsToPlayer), to change the bool when the C key is pressed. This method is being called every frame in void Update.

I use GetKeyDown to send a single message when the C key is pressed. That message changes the boolean value to true. The else if part of the argument changes the value back to false when the C key is released.

In order to send this value to the powerup, I create a public bool method with a return value, so the powerups can access it to check the value. This method needs the return keyword, or you will get an error telling you this method needs a return value.

I just noticed I mis-spelled powerups in my isPullingPowerups variable name. Ugh! I will fix that.
Moving over to the Powerup class now, I add a variable to mirror the public boolean value being sent by the Player.

I already had a handle to the Player and a GetComponent in void Start, but I thought it was worth mentioning again here, being this is needed for the Powerup to check the public value on the Player.

The Update method in the powerup class is really simple, with only a movement method for now.

In the PowerupMovement method, I first check to see if the player is null. Then I assign the value of the bool (isPlayerPullingPowerups) in the powerup class, to equal the value of the bool being passed from the player through the public void (PoewerupPullCheck). Now I use an if else argument with conditions for entering the if statement. The player must not be null, to ensure I don’t throw a game breaking error, and the isPlayerPullingPowerups value needs to be equal to true. The Powerup then uses a Vector2.MoveTowards method to draw powerups to the player.
The else statement holds the standard behavior for the powerup when it’s not being summoned by the player. I also added a new speed variable at the top of the powerup class to move the powerup faster than normal.

Here is a clip of the new Player ability, drawing powerups to itself. Thanks for reading!

Giving the Player Telekinesis 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 Jared Amlin
Jared Amlin | Sciencx (2021-08-02T03:07:02+00:00) Giving the Player Telekinesis. Retrieved from https://www.scien.cx/2021/08/02/giving-the-player-telekinesis/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.