Get Over Here: Tractor Beam (Game Dev Day 42)
Objective: When the player presses the “C” key, nearby Powerups will pulled towards the player.
After the implementation of the homing missile system, this is a piece of cake to add into a game.
First I create a new GameObject (_tractorBeam) and make it a child of the Player Gameobject. I used a simple circle sprite to give a visual effect when the tractor beam is active.
When active, it looks like this.
Next on the Player.cs script I added the following code to void Update:
By default the _tractorBeam will be turned off unless the player is inputting the “C” key.
Right now this is only a visual effect. To give it actual function I made the following changes to my Powerup.cs script.
First I made three new variables: a public float value to check the distance from the powerup to the player, the physical location of the player, and a force to apply to the RigidBody2D on the powerups themselves.
I then added the following to void Update.
I set up the float variable (_distance) to measure between the powerup and the Player. Then, on line 28, if the player is holding down “C” and they are within 5 units the powerup will then attract towards the player.object.