Trying to create the experience of magnets in a VR environment using Unity.
Background
While playing with my daughter’s magnetic building blocks I found myself wondering whether I could recreate the experience in VR. Nothing too complicated, just simple building blocks that behaved like magnets.
I had already started playing with physics in Unity, especially adding Force to objects, so I figured it would be pretty straight forward.
Fortunately, it was and in no time I had created a simple script that would check for other magnetic objects nearby (objects that were tagged “Magnetic”) and pull them towards themselves (by adding force in the opposite direction).
I later added the option to change the direction of force to either attract or repel them via a simple drop-down in the editor and added sliders to easily change the radius and strength of the “magnetic field”.
I also added some spheres to the mix and colour-coded the objects: green for pull and red for push.



