Problems with Magnet Setup

Hi all!

I’ve had no luck in getting the spheres in the attached sim to “snap” onto the cylinder so that the cylinder goes through the middle of the spheres at all times. The spheres either “stick” to the bottom or the top of the cylinder. When I start dragging them around they end up all over the place. I’m using the following code to get the “magnetic” effect:

var force = -300.00;
var range = 100.00;
var player : GameObject;

function Start () {
   player = GameObject.FindWithTag("Player");   
}
function FixedUpdate () {
   if(player) {
       player.rigidbody.AddExplosionForce (force, transform.position, range);
   }
}

I’ve attached the code to the cylinder with a separate script for each player tag, i.e. “player”, “player2” and “player3”.

I’d be grateful for any pointers about where I’m going wrong! Thanks!

121036–4574–$magnet_test_512.unitypackage (8.3 KB)

number of things you could try increase the mass of the Rigidbody object, turn freeze rotation on increase or decrease the force all or the above in some degree should help you achieve the desired effect …

number of things you could try increase the mass of the Rigidbody object, turn freeze rotation on increase or decrease the force all or the above in some degree should u achieve the desired effect not much wrong with the code…

Thanks Solventfactory!

I’ve tried so many permutations that my head is spinning. If there’s a magic combination, I haven’t found it yet! I often seem to get close only for a sphere (or the whole bunch) to end up sticking to the underside of the cylinder on the next drag and drop.

I was hoping to get it set up so that the spheres could easily be dragged to different positions along the cylinder but snap into position smartly on mouseUp.

Is there another approach I could try? Maybe I should use more than a single cylinder so that I have a bunch of colliders and then use “is Trigger” to snap to a standard set of coordinates each time?

Hi bud without getting my hands dirty and looking at it meself can’t help u myself but my Internet is down using my iPhone but if your still pulling your hair out next week n I can spare a bit of time and my internet is back online will download but don’t worry your on the right track I lost a bit of hair when I was working out how to get magnet rigidbodys to work :smile:

Not sure if this will help or if this will make sence but here goes…create a box object position it in such a way that it blocks the incoming object from intersecting make it invisible give it a collider and make it a child of the object that is the magnet that might help also give the object that is being attracted to the magnet a high mass and turn rotarion off in the rigidbody check box hope you understand what I’m getting at good luck

Thanks Solventfactory! You put me on the right track. I’ve basically “boxed” the spheres and cylinder inside a set of cubes with colliders to keep them in position.

The front cube’s “Layer” stetting is set to “Ignore Raycast” so that I can still click and drag the spheres. The only irritating aspect is that when you drap and drop from close range onto another sphere it doesn’t always move away to allow the dragged one onto the cylinder but often lets the other sphere intersect it and stay there.

no worries glad i could Help… :smile: