ConfigurableJoint does not move when hooked up dynamically.
Hello, I'm brand new to Unity and I'm just playing around with a few ideas. I am currently trying to just make a tractor hook up to a trailer via a ConfigurableJoint. I have the joint hooked up to the trailer and an invisible collider on each tractor and trailer to allow me to know if I'm "close enough" to hook up the trailer.
Once I get close enough, I am able to check what collider is being collided with then I press a button (tab in this case) and the ConfigurableJoint hooks to the tractor.
My issue is that when I hook the trailer up to the tractor, the configurable joint wants to stay in place until it's bumped. I use the tractor to back up into the trailer to bump it just a bit and then everything works as expected. I have everything set to zero, but something must be missing. Below is everything I'm doing:
This script checks the collider, if the collider is the right one, I can press tab and I check whether it's connected or not (with a 1 second delay). Then if I were to hook the trailer up, I give the trailer's connected body the appropriate rigidbody (this might be where the issue is). I also set all of the motions to locked so there's no springyness to the trailer.
void OnTriggerStay(Collider collider) {
if (collider.name == "HitchColliderTractor") {
if (Input.GetKey(KeyCode.Tab)) {
if (!once) {
once = true;
Invoke("revertOnce", 1F);
if (!trailer.connectedBody) {
trailer.connectedBody = collider.GetComponent<Rigidbody>();
lockTrailer();
} else {
trailer.connectedBody = null;
unlockTrailer();
}
}
}
}
}
void revertOnce() {
once = false;
Debug.Log("Revert");
}
void lockTrailer() {
trailer.xMotion = ConfigurableJointMotion.Locked;
trailer.yMotion = ConfigurableJointMotion.Locked;
trailer.zMotion = ConfigurableJointMotion.Locked;
trailer.angularXMotion = ConfigurableJointMotion.Locked;
trailer.angularYMotion = ConfigurableJointMotion.Locked;
trailer.angularZMotion = ConfigurableJointMotion.Locked;
jackPos = jack.transform.localPosition;
jack.transform.localPosition = new Vector3(jack.transform.localPosition.x, 0, jack.transform.localPosition.z);
}
void unlockTrailer() {
trailer.xMotion = ConfigurableJointMotion.Free;
trailer.yMotion = ConfigurableJointMotion.Free;
trailer.zMotion = ConfigurableJointMotion.Free;
trailer.angularXMotion = ConfigurableJointMotion.Free;
trailer.angularYMotion = ConfigurableJointMotion.Free;
trailer.angularZMotion = ConfigurableJointMotion.Free;
jack.transform.localPosition = jackPos;
}
Below is a pic of the joint settings as default:
When the trailer hooks up, the rigidbody changes and all of the motions change properly. But for some reason the joint sticks in place until it's bumped. Here's a gif to show what I mean:
http://images.myfpv.io/Trailer_hitch_issue.gif
When I hook the tractor to the trailer (you can see when the hitch arm moves up), the joint doesn't move. The tractor doesn't have enough power to pull the trailer and actually gets pulled back like it's on a strong bungee. If I back up and bump the trailer a tiny bit, the joint moves and everything works perfectly.
Any thoughts? Thank you!
,Hello, I'm brand new to Unity and I'm just playing around with a few ideas. I am currently trying to just make a tractor hook up to a trailer via a ConfigurableJoint. I have the joint hooked up to the trailer and an invisible collider on each tractor and trailer to allow me to know if I'm "close enough" to hook up the trailer.
Once I get close enough, I am able to check what collider is being collided with then I press a button (tab in this case) and the ConfigurableJoint hooks to the tractor.
My issue is that when I hook the trailer up to the tractor, the configurable joint wants to stay in place until it's bumped. I use the tractor to back up into the trailer to bump it just a bit and then everything works as expected. I have everything set to zero, but something must be missing. Below is everything I'm doing:
This script checks the collider, if the collider is the right one, I can press tab and I check whether it's connected or not (with a 1 second delay). Then if I were to hook the trailer up, I give the trailer's connected body the appropriate rigidbody (this might be where the issue is). I also set all of the motions to locked so there's no springyness to the trailer.
void OnTriggerStay(Collider collider) {
if (collider.name == "HitchColliderTractor") {
if (Input.GetKey(KeyCode.Tab)) {
if (!once) {
once = true;
Invoke("revertOnce", 1F);
if (!trailer.connectedBody) {
trailer.connectedBody = collider.GetComponent<Rigidbody>();
lockTrailer();
} else {
trailer.connectedBody = null;
unlockTrailer();
}
}
}
}
}
void revertOnce() {
once = false;
Debug.Log("Revert");
}
void lockTrailer() {
trailer.xMotion = ConfigurableJointMotion.Locked;
trailer.yMotion = ConfigurableJointMotion.Locked;
trailer.zMotion = ConfigurableJointMotion.Locked;
trailer.angularXMotion = ConfigurableJointMotion.Locked;
trailer.angularYMotion = ConfigurableJointMotion.Locked;
trailer.angularZMotion = ConfigurableJointMotion.Locked;
jackPos = jack.transform.localPosition;
jack.transform.localPosition = new Vector3(jack.transform.localPosition.x, 0, jack.transform.localPosition.z);
}
void unlockTrailer() {
trailer.xMotion = ConfigurableJointMotion.Free;
trailer.yMotion = ConfigurableJointMotion.Free;
trailer.zMotion = ConfigurableJointMotion.Free;
trailer.angularXMotion = ConfigurableJointMotion.Free;
trailer.angularYMotion = ConfigurableJointMotion.Free;
trailer.angularZMotion = ConfigurableJointMotion.Free;
jack.transform.localPosition = jackPos;
}
Below is a pic of the joint settings as default:
When the trailer hooks up, the rigidbody changes and all of the motions change properly. But for some reason the joint sticks in place until it's bumped. Here's a gif to show what I mean
http://images.myfpv.io/Trailer_hitch_issue.gif
When I hook the tractor up, if I don't bump the trailer, the the joint stays there and the tractor doesn't have enough "power" to pull it, so it actually gets pulled back like it's on a bungee cord. once I back up and bump the trailer, the tractor pulls it just fine.
I hope this is formatted alright, first post on here.
Answer by EvilBob99 · Nov 06, 2021 at 04:51 PM
I think if im reading this right you need the movement unlocked and some of the rotation because from what im reading your making unable to move at all which means it has to be in the perfect spot for it to work @ntgCleaner