Magnet pulling 2d

hello people! i would like to have some help according to my current project!
i would like to have a player object in the middle.
and some magnet objects in my scene that will listen my tap on them and pull my player to their side… magnetize “him” …
thanks in advance

i would like to have some help creating and scripting the magnet “pullers”

Find the distance from the player to the magnet

If they are inside the magnets range, divide the diatnce by the magnets range to get a value between 0.0f and 1.0f; Basically a percentage value.

Decide on a strength for the magnet, and multiply that by the percentage value you have just calculated.

Apply the resulting force to your player (assuming you are using a RigidBody).

1 Like

sorry…but, magnets will be colliderss i guess?
am i right?
and the collider will be bigger than the actual magnet so this will be the magnet’s range??
sorry but im new to unity!
thanks you very much

You could do colliders, or if there’s not many on screen just check the distance in update or something. And ya, @Strategos is spot on. That’s exactly how I would do it.

Using triggers make sense if you have complex scenes with many magnets and many things being attracted to them. PhysX does some pretty good optimisations behind the scene for you.

If there are only one or two magnets affecting only the player then you are better off doing straight distance checks.

Unity5 has point effectors,

in action: