Making a magnet

Hello my question is about setting up a magnet system. What am trying to do is make enemies act as magnets. So player will get pulled in the direction of the enemy. This wouldn’t take away player control they should still be able move in the opposite direction of the enemy just at a slower rate. If they move towards the enemy that’s pulling them they will also move at a faster rate. Been trying to get this working for a few days and haven’t been able to find a good solution.

Maybe this will work?

Every Tick, if player is within a minimum radius from the enemy, then AddMovementInput in the Lookat direction unit vector toward the enemy, to that players’ playercontroller, multiplied by that minimum radius minus distance, times an adjustment factor of your choice.

If you need them to be able to be pulled off the ground or faster than they can move away, then this will not work.
There is an instantaneous effect using LaunchCharacter but might not be smooth enough (can choose add velocity or set velocity though)

Might have to use something like the first approach I mentioned but just add velocity to the actor. However this doesnt pull them off the ground into the air if that is what you want.

Just some ideas. Not sure what works best outside of blending with physics simulation.

Assuming you are using characterpawn instead of other type of pawn.

so adding movement input sort of works 2 things I’ve notice tho if am on the left side of the enemy it pushes away from the enemy (think that maybe something to do with paper2d) and is there a way I can increase the speed of this?

Are you using the LookAt vector and not just subtracting the vectors?

the adjustment factor you multiply the whole thing by can be a float variable and you can make it bigger to get a more dramatic effect.

You probably still want it to be stronger the closer you are to the enemy so leave that part in too. Make sure your distance to the enemy is passed through an ABS node so it doesn’t get flipped on opposite side.

what kind of math would I have to do? and why avoid using event tick?

Try utilizing Launch Character using an event by timer. Set the timer to .001 and trigger that once the
Player character overlaps a collision box attached to the enemy. May require a little more math to determine which direction the player character will gravitate towards. Avoid using event tick.

alright switched it over to use look at function and it’s working pretty good actually almost got all the functionality in tho I do notice it’s really affecting the players speed and not really able to walk away from it

is the look at vector a node? all am seeing is look function and find look look at rotation and currently using get unit direction.

A timer that short would fire more often than tick unless your framerate is very high, but maybe that is the point: to make launch character smoother.

launch Character has the further benefit of working smoothly across network even when called from client side.

Just use the look at function from the player location to the enemy location and multiply it by the inverse of how far away the enemy is.

You’re halfway there. Now it is just a matter of tweaking values until you get the results you want. Magnets in real life seem to pull on an exponential scale instead of a linear one so your distance factor could be used as an exponent instead of just multiplying. That way when far away it is very weak. Medium distance still weak but a little stronger, close is medium strength and then really close is super strong all of a sudden.

so using launch character seems to be kind of buggy it will start pulling towards the then launch the player across the map

here is a picture of my math feel like I may be doing this wrong

alright this seems to be easiest solution to use just tested in game and works pretty good thanks for the help

No problem! From here you should be able to tweak it to your liking. You can use the timer by event for a ton of different situations. I find it to be a good alternative to event tick.

Debaucher you should convert your comment to an answer :slight_smile:

Here is a simple version of what you are trying to achieve. I used the Third Person template provided by Epic. Created a Blueprint Actor, added a sphere (labeled as enemy) under the default scene root, added a box collision under the default scene root (not attached to the sphere).

Good call, Enigma. Thank you!