Magnets and Unity — how to make an object come to you!
Ok I’m coming clean right from the start. This challenge really threw me for a loop. I’m not sure if it has to do with the pages of code I already have in this game or if it’s time for me to move on to another game. But man, this took forever! I want to give a big shoutout to my colleague Connor Fullarton who wrote this article to get me over the hump!
So, what threw me for such a loop? Giving the player the ability to press the C key and have powerups come to him. This actually isn’t that difficult once I saw how it worked but man…
So how did I (see: Connor) do it? See below:
So in the Player script all that had to be done was create a new function (above) and call that function in the update. So what does it actually do?
First, you need to make sure all of your powerups share the tag “PowerUp”. I did this in my previous article when I had the enemy shooting the powerups so this was all good. Then we want to get the number of powerups that are on screen so that our for loop will know how many times to run.
Fun side bar here — I left the powerdown on here as well. So if the powerdown is active and the player hits C it will suck that towards him as well as the powerup. This made a sort of “mini-game” when I had to move my ship away from the powerdown so I didn’t gather it as well.
Any way, so when the for loop runs it is going to get the powerups position and change it using the MoveTowards that will push it towards the players position.
Let’s see this in action:
As you can see the player has to be careful when pressing C so the powerdown doesn’t come as well. Cool stuff — even more tactics for our player to consider!
So I’m up in the air about the final implementations. I have a boss I want to put in so after wave 3 the player has to fight it. I’m still thinking about making the enemy have more brains and try to dodge the player laser.
Sometimes you just have to say enough though. I think my game is at a really good place as is. Well, I guess you’ll find out tomorrow! Please join me to see which way I head.