• News

• Industries

• Learning & Support

• Community

• Marketplace

# Magnet

Hello,

I’m creating a endless runner and want a power up that attracks all the coins (blueprint). I tried some things and searched for tutrials but cant find any. The theory should be lik change the gravity to my player so that the coins fall to me. Does anybody has an idea how to create that in blueprints?

Hi Mate,

When you go over the box collision where they spawning, do simple move to character of the coins

Narg

Can you have an example? I tried to do it but it doesnt work.

thanks Narghile

It’s not possible to that such interesting thing has a very simple solution:)

I’ll try here with some collectible objects

When you spawn, set them in an array.

You may need 7 arrays, not big for this games.

when begin overlap, get the overlap, compare to array, then do move to.

It is a little coding, but possible

Narg

It was quite simple to make actually. If you have followed the Unreal infinite runner tutorial which I assume you have as you are trying to replicate his functionality.

You want to do a find all actors of class “coins”. Then from that selection check their distance from the player. If they are within your set distance, then move their position towards the player.
Here is a fairly simplified and messy way of doing it. You can see I am checking if the coin is within 400 units of the player and then if it is I move it towards the players location at 120% of the player speed, this is because my player speed goes up when I collect coins and I don’t want the player moving faster than the coins or they would never get to him.

You could probably optimize this a lot better then it currently is but it did the job for my very quick version.

You could set a “RadialForce” under your player actor with a negative value. That should work I think. Or even just increase the coins hitbox, or the players, so they hit the coins earlier, depending on how you want to do it.

That is an awesome idea, and it works great!
Thanks a lot

I gave the radial force 1 a try as well but once the player speed starts to get too high it was ineffective. In mine though I had an infinite top speed though so if you had a cap then you probably wouldn’t have a problem.

I know this is semi old, but it’s what pops up when Googling it. I don’t find these solutions very elegant so I took a stab at it. I’m not a huge fan of continuously looping through all the coins on the map.

It’s incredibly easy, moreso than the posts above. Simply going into the coin’s tick event and checking the distance between it and the character seemed best. If it’s within range, a simple “Move Component To” over .25 seconds works wonders. Add a delay at the end (.25s) so you’re not executing this more than necessary, and you’ve got yourself a super easy magnet. I’m not sure when “Move Component To” was introduced, so you may need to just manually set the location with some vector math, but that shouldn’t be too hard either.

If you want to prevent the coins from going behind the player character, add the velocity / 5 to the player character location.

If you also want to add a magnetism variable, add the float to the player character. Now the “Over Time” parameter on “Move Component To” should be .25 / magnetism. Make sure this code only executes if magnetism is > 0. Try it out, .5 will collect most but not all, 1 will collect nearly all, and anything above will rapidly collect.

Now, for one final touch that I applied, I also use “Add Angular Impulse” for a cool effect when the coins are being magnetically pulled to the player. I’ve got my rocks and coins simulating physics to make sure coins never end up inside rocks (with Z locked so they don’t go flying out of bounds), and also use “Add Angular Impulse” in the construct of the coin so they all rotate at different rates and angles.

Hope this helps give some ideas, and I hope it helps those who are looking at this a little later in the game (like me).

Here’s an image of what I’m talking about in its simplest form. Note, I’m using cylinders for my coins so yours will vary.

your solution worked, but I encountered another problem.

Since the coin is generated by the platform, the relative location seems to be reffering to the platform and not the world. The result - as soon as my character passes close enough to the coins (withing range) the coins rush towards the player location…relative to the platform and not to the world. Basically they just rush ahead for miles.

Any idea on how to fix that? I just picked up UE a few days ago. I figured it out how to work with blueprints, but it will be a long time before I build up the “vocabulary” of functions

Got it working (sort of) by using Inverse Transform Location. But it’s weird, the magnet first sort of repels the coins then sucks them in. And as soon as you turn a corner, the vaules are off. I guess there has to be some rotation involved aswell as position and it was getting just TOO complicated

Got it working perfectly with the first solution, a few posts above! needed some tweaks but got it working, eventually, and my blueprint looks like a child’s doodle now! :))

If anyone else gets stuck drop me a PM, I might be able to help.

Cheers!

I’ve tried both solutions, and the 2nd one does the same as yours. It seems to push the coins away at a ridiculous speed. I cant get the first method to do anything. Could you please Tell me what tweeks you made to it.
Thanks