- Home /
Configurable joint set to limited locking when connected.
Hey everyone, I have a problem that I'm pulling my hair out over and I've spent 10 hours or so trying to fix it, the problem is simple. I am creating a VR game I have a mag for a gun and I am trying to create a script that allows me to slot it in using a configurable joint. I create the joint at runtime when I connect the gun and mag, I have the joint set to limited on the x-axis but for some reason, it locks and won't slide, I've done a bunch of logging, checked my colliders (even though that shouldn't matter because joint collision is off.), and the script is doing exactly what its meant to do. The problem is fixed when I let go of the mag, I'm holding it using a separate configurable joint and no code is being called that interacts with the mag in any way except that the configurable joint connecting it to my hand is destroyed. After letting go the joint works as expected and can slide in and out of the gun perfectly. I can even grab the mag again after letting go and it will still work.
So to recap, my problem is that an unlocked joint locks when I create it and connect it to something, disconnecting a previously connected joint fixed the problem but does not appear to be the source of the problem as the mag can be connected without being held, the joint then functions normally.
Here is my code for creating the joint:
SetupJoint();
locked = false;
private void SetupJoint()
{
Debug.Log("creating joint");
joint = gameObject.AddComponent<ConfigurableJoint>();
joint.anchor = new Vector3();
transform.rotation = currentSlot.transform.rotation * Quaternion.Inverse(slotPoint.localRotation);
transform.position = currentSlot.transform.position - (slotPoint.position - transform.position);
GetComponent<Rigidbody>().rotation = currentSlot.transform.rotation * Quaternion.Inverse(slotPoint.localRotation);
joint.connectedBody = currentSlot.parentRigidbody;
joint.autoConfigureConnectedAnchor = false;
joint.connectedAnchor = currentSlot.transform.localPosition - Quaternion.Inverse (currentSlot.parentRigidbody.transform.rotation) * (slotPoint.position - transform.position);
joint.connectedAnchor += currentSlot.transform.localRotation * Vector3.up * currentSlot.depth;
joint.xMotion = ConfigurableJointMotion.Locked;
joint.zMotion = ConfigurableJointMotion.Locked;
joint.yMotion = ConfigurableJointMotion.Locked;
joint.angularXMotion = ConfigurableJointMotion.Locked;
joint.angularYMotion = ConfigurableJointMotion.Locked;
joint.angularZMotion = ConfigurableJointMotion.Locked;
joint.axis = currentSlot.transform.localRotation * Vector3.forward;
SoftJointLimit limit = joint.linearLimit;
limit.limit = currentSlot.depth / 2;
limit.contactDistance = 0;
joint.linearLimit = limit;
joint.projectionAngle = 3;
joint.enablePreprocessing = false;
joint.projectionMode = JointProjectionMode.PositionAndRotation;
}
public bool locked
{
get
{
return joint.xMotion == ConfigurableJointMotion.Locked;
}
set
{
if (value == (joint.xMotion == ConfigurableJointMotion.Locked))
return;
if (value)
joint.anchor += currentSlot.transform.localRotation * Vector3.up * currentSlot.depth / 2;
else
joint.anchor -= currentSlot.transform.localRotation * Vector3.up * currentSlot.depth / 2;
joint.xMotion = value ? ConfigurableJointMotion.Locked : ConfigurableJointMotion.Limited;
//Debug.Log("locked");
}
}
Your answer
Follow this Question
Related Questions
Physics not working with Character Controller 1 Answer
Strange physics, rigidbody, iTween activity 1 Answer
Unity 5 Freeze Rotation Bug 0 Answers
Configurable Joint just falls apart? 1 Answer