How to have a rigidbody object stick to another object?

I have been looking into magnetism/gravity but 2/3 scripts didn’t do anything (I tried messing around with values) and with the 3rd, the balls were attacted to weird a place in my level instead of the desired object. Perhaps Unity 2.6 broke that.

Basicly I want a player controlled ball to stick to an object like glue/magnetism.


Would this be possible at all with these kind of shapes and any pointers on how to accompish this?
I doubt gravity is the way to go for more complex shapes like this?

For the 2nd image I also might need to aply a vertical force?

I think you’d be best off turning off gravity altogether (set it to zero).

The way I’d approach this would be to send out a series of raycasts from your sphere, and apply a force based on the results. You could either apply a force based on averaging the direction of all the hit results, or just take the nearest hit result and apply a force in the direction of that surface.

To generate the ray directions in the first place, you’ll need an algorithm to generate roughly evenly distributed points on your sphere. Looks like there are good examples here:

http://www.xsi-blog.com/archives/115

good luck, sounds like a fun project :slight_smile:

Thanks it got me going,

I am trying to create a trigger_magnetic script that is attached to the structure.
The onCollisionEnter function grabs the position of a collision point and with that information I somehow have to find out a angle that I can use to update the gravity settings.

OnCollisionExit is used to return the gravity setting back to normal when the player leaves the structure.

This is what I have so far:

private var collisionPoint : Vector3;
private var playerPosition : Vector3;


function OnCollisionEnter(collision : Collision) 
{
	for (var contact : ContactPoint in collision.contacts) 
	{

		Debug.Log ("point:");
		Debug.Log (contact.point);
		Debug.Log ("normal:");
		Debug.Log (contact.normal);
		collsionPoint = contact.point;
	}
	

	//playerPosition = otherCollider.transform.position;
	
	var angle = Vector3.Angle(collisionPoint, playerPosition);
}

But I don’t need the for loop, I tried a bunch of things like var contact : ContactPoint = collision.contacts but I can’t figure it out.
I’m also not sure if the angle will be a useable value yet :slight_smile:

Have you tried applying a force to the ball in the opposite direction to the contact normal?

No I asume that would work too.

This works just perfect:
http://forum.unity3d.com/viewtopic.php?p=222527#222527

Attached the final script.

Now I just want the For loop gone, there is only one contact at a time I think?
But I can’t find a way to acces the normal in any other way, I tried these:

collisionNormal = collision.contacts.ContactPoint.normal;
collisionNormal = collision.contacts.normal;
collisionNormal = collision.ContactPoint.normal;
collisionNormal = collision.normal;
collisionNormal = collision.ContactPoint.normal;

217956–7933–$trigger_magnetic2_549.js (1.65 KB)

To only operate on the first contact point in the list:

Physics.gravity = collision.contacts[0].normal * gravityStrength;

There’s usually only one, but there can valid situations where there are more - eg, if the ball is in a corner. The most correct thing to do would probably be to average the result of them all.