Hi everyone,
I’m working on a level generator for an endless runner and I want to spawn some prefabs with hinge joints.
The trouble is that when I spawn an object with a hinge joint, it retains the anchor and connected anchor from when the prefab was saved, not relative to its spawned position.
Any idea how I can fix this?
I am spawning a hinge joint object from a prefab and it works OK, provided you connect to the other rigid body after instantiation.
objectBwithHingeJoint = GameObject.Instantiate(hingeJointPrefab, objectA.hingeJointSpawnOffset.transform.position, Quaternion.identity) as GameObject;
HingeJoint hj = objectBwithHingeJoint.GetComponent<HingeJoint>();
hj.connectedBody = objectA.rigidbody;
You should also be able to adjust the anchor properties via script at this point.
Also I found early on that nesting an object with a hinge joint inside the object its attaching to caused lots of problems. So I spawn them both independently (with B getting its initial position from a dummy transform inside of A) and attach via script, as you can see above.