Keeping Rigidbody Within Distance From Point

I’m trying to keep a rigidbody from moving beyond a certain distance from a point. I’ve tried using joints, but the don’t allow the rigidbody to move freely within the radius- they snap back to their initial position.

Does anyone have any ideas?

You might be able to do something with this:

So you calculate the vector from the point to the rigidbody and then you limit the maximum magnitude/length/distance that is allowed between the point and rigidbody :slight_smile: And you just do that in Update() or maybe in FixedUpdate(). I don’t know if this will work well with a rigidbody with physics applied to it though…

I think a configurable joint with Linear Limit set to your radius would do what you want.

Hmmm. That might work, but you still have to deal with physics…

Tried that. Problem is the radius is from the initial position of the attached object, not the point I want the system to be limited to.

Have you set the Connected Anchor to be the point you want to circle?

(Sorry for late response)

Yes. It works if I create the joint in the editor, but if I do it via script, it doesn’t constrain the object. Code:

					//Stick Tether
					tetherTransform.position = hit.point;			
					tetherIsExtending = false;
					tetherIsMoving = false;	
					
					tetherTransform.rigidbody.mass = 800;
					
					myJoint	= tetherTransform.gameObject.AddComponent(ConfigurableJoint);
					myJoint.anchor = Vector3(0,0,0);
					myJoint.axis = Vector3(0,0,0);
					
					myJoint.linearLimit.limit = maxTetherRange/2;
					myJoint.linearLimit.spring = 0;
					
					myJoint.xMotion = ConfigurableJointMotion.Limited; 
					myJoint.yMotion = ConfigurableJointMotion.Limited; 
					myJoint.zMotion = ConfigurableJointMotion.Limited; 
					
                                        //These were the editor defaults, but they default to zero if the joint is created via script
					myJoint.xDrive.maximumForce = 3.402823e+38;
					myJoint.yDrive.maximumForce = 3.402823e+38;
					myJoint.zDrive.maximumForce = 3.402823e+38;
					
					tetherJoint = hit.transform.gameObject.AddComponent(FixedJoint);
										
					tetherJoint.connectedBody = tetherTransform.rigidbody;
					myJoint.connectedBody = myRigidbody;