- Home /
Hinge Joint? Unconstrained.
Basically I want to have a connection between two (preferably not rigidbodies but if it needs a joint I can make a workaround) GameObjects. think of a rope, but it doesn't have to be rendered or have segments. When the two objects are connected if one object moves past the rope length it will either pull the connected object with it (if the other object isn't static) or pull the object back to the connected object.
Answer by Bunny83 · May 26, 2011 at 10:02 AM
All joints only works with rigidbodies since they have to add forces to both attached objects. You can set one of them to be kinematic, but the other one have to be non kinematic. Otherwise it will bypass physics simulation.
A Hinge Joint isn't a slider. it normally have a constant length (which can stretch a bit due to spring property). It sounds like you want just to limit an object to move further away.
If you don't use rigidbodies and joints, you can handle the case yourself. Is the one point at a fix position? If so the object that moves away just needs to check the distance and move it back until the distance is ok.
//C#
public Transform fixPoint;
public Transform movingPoint;
public float maxDistance = 10.0f;
void Update()
{
Vector3 dir = fixPoint.position - movingPoint.position;
float dist = dir.magnitude - maxDistance;
if (dist > 0)
{
movingPoint.Translate (dir.normalized * dist * Time.deltaTime,Space.World);
}
}
If both points can be moved it gets a bit tricky. If you want the same behaviour like the HingeJoint you just need to affect both points (the second point just in the opposite direction). That would enable one point to pull the second point towards he's moving.
If it should not pull the other object the above script should only be executed for the point that is moving. If both can move you have to switch the two points.
Well I can simply parent the object to a temporary rigidbody if using joints is easier.
But yeah, I want a hinge joint that ins$$anonymous$$d of keeping the object at a certain distance, keep it in a certain distance. So if the object is moving toward the object it is connected to, it will move freely until it get to the max distance. (at which point they will spring together)
I thought a spring joint would be easier but they are retarded. (when you create one it uses the starting positions ins$$anonymous$$d of the current ones). P.s. I was hoping for it not to "snap" back but for it to be pulled back.
If you want it that way you can use the script above. It behaves like a spring. The "force" grows proportional to the distance, but only if it's greater than the maxDistance. You can move the object within the sphere with a radius of maxDistance freely. Only if it gets outside this script will "pull" it back. If you don't want the smooth return over time you can use LateUpdate and remove the Time.deltaTime. That way it's a hard stop at the sphere border.