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.