Any ways to make a magnet effect?

Hello people!

I would like to make a “magnet” effect. Basically, make it so a part gets attracted to a part.
Any ideas of how to do that? (Please note: The part is unanchored, and to be more precise, it’s the HumanoidRootPart)

Thanks in advance!
-Mehdi_ea

5 Likes

I found the easiest way to do this is to have a table that consists of all of your parts that you’re looping through, and then check the distance between your HumanoidRootPart and the part you’re attracting.

1 Like

Good idea. Will try it tomorrow, since i’m tired right now, and I don’t feel like making a huge script. I’ll keep you updated.

1 Like

An even easier way is to place an attachment into each part and place AlignPosition into the part you want to move. Set the moving part’s attachment to Attachment0 and the target part’s attachment to Attachment1.

3 Likes

We can use a simple point charge to model our magnet (as more accurate models for magnetic-force is a bit too complex for beginners). The force equation for 2 point charges is F=k*(q1*q2)/r^2. You can set the ‘charge’ of the parts to whatever you want. Use a BodyForce and have an update loop which calculates the Force Vector (pointing directly at the other part) based on distance and charges of the 2 parts.

4 Likes

Hi!

Yes, that is a 6 months old topic. Yes, I am sorry to revive it.
Today, I tried to get better physics knowledge. Well, found that wonderful constraint that is the AlignPosition Constraint. That totally solves my question.

Thanks for all the answers.

5 Likes

Lerping might also work with it.

1 Like

Hey how do i make a magnet effect in roblox. This time i only want it to affect metal.

And suggestion to Roblox:
You should make a Magnet
You can make it so you can turn any metal part into a magnet so it can attract or repel any other unanchored metal parts.

Hi - you probably want to pair an AlignPosition with a script aswell. Either way, consider starting a new topic in scripting support, it’s better than reviving old topics

Good day to you!

Ok, I should use AlignPosition, with a script
And about your advice, sometimes DevForum won’t let me post a new topic every time i have a problem.