- Home /
Keep a certain distance between two RigidBodies
I'm working on a grappling hook, and I want to keep a certain distance between two RigidBodies, while still letting one orbit the other. An example of what I'm looking for is the grappling hook in Floating Point. The 'rope' is rigid and has no give.
My current solution is to apply a force to keep it a certain distance away, but it's really wobbly. Is there any way other than joints to do this?
if (Vector3.Distance(transform.position, contactPoint) > distanceToGrapplePoint) {
rigidbody.AddForce(contactPoint - transform.position);
} else if (Vector3.Distance(transform.position, contactPoint) < distanceToGrapplePoint) {
rigidbody.AddForce(transform.position - contactPoint);
}
Answer by Kiwasi · Aug 27, 2014 at 08:02 PM
The correct way to do this is joints. There is no valid reason to be avoiding joints.
The solution you have coded is equivalent to a spring joint, where the force added is proportional to the distance away from the target position.
What you are actually after is a fixed joint. On fixed joints any force applied to either object is spread out across both objects. That's a lie, but you get the idea.
In most cases its easier to simply learn and use Unity's built in physics system, as opposed to writing your own. Joints are not that hard.
Your answer
Follow this Question
Related Questions
Handling colliders on hundreds of asteroids (not procedural asteroids) ...URGENT 3 Answers
Simple way to get all objects (rigidbodys) around x distance from the player 1 Answer
Restrict two objects from getting too close. 1 Answer
Calculate where an object is going to land 0 Answers
Why Does My Prefab Fly Upwards When Moving Around? 0 Answers