- Home /
Create hinge joint 2D by collision
I have a script which destroys a 2D hinge joint. To recreate the joint between two specific gameobjects I figured using a collision or trigger would work, but I cant seem to get any progress on recreating the joint (and thus allowing my script to destroy it on input key and start the process over). Any ideas on how to do this in script?
Answer by Duss · Jul 01, 2014 at 04:33 AM
If you want to recreate the joint on collision, this should works:
void OnCollisionEnter(Collision other) {
if (other.gameObject.tag == "YourOtherObjectTag") {
HingeJoint myJoint = (HingeJoint)gameObject.AddComponent("HingeJoint");
myJoint.connectedBody = other.rigidbody;
}
}
Add 2D after everything. OnCollisionEnter2D, Collider2D, HingeJoint2D ect.
In general 2D physics is 3D physics that ignores one dimension.
It does work! marvelous. I had forgotten about tags. Thank you Duss, and Bored$$anonymous$$ormon for assisting me and helping me further my hobby
but now whatabout adding anchors via script?
Answer by mcmustang51 · Jul 03, 2014 at 03:51 AM
void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.tag == "player") {
Debug.Log ("hit");
HingeJoint2D myJoint = (HingeJoint2D)gameObject.AddComponent("HingeJoint2D");
myJoint.connectedBody = other.rigidbody;
}
}
no collision is detected is now my issue. the debug log never shows anything. any thoughts?