/// <summary> /// Sets the <see cref="jointContainer"/> position based on the joint anchor. /// </summary> protected virtual void SetJointContainerPosition() { // Disable any child rigidbody GameObjects of the joint to prevent the anchor position updating their position. Rigidbody[] rigidbodyChildren = joint.GetComponentsInChildren <Rigidbody>(); bool[] childrenStates = new bool[rigidbodyChildren.Length]; for (int i = 0; i < rigidbodyChildren.Length; i++) { if (rigidbodyChildren[i].gameObject == jointContainer || rigidbodyChildren[i] == jointRigidbody) { continue; } childrenStates[i] = rigidbodyChildren[i].gameObject.activeSelf; rigidbodyChildren[i].gameObject.SetActive(false); } // Set the current joint container to match the joint anchor to provide the correct offset. jointContainer.transform.localPosition = joint.anchor; jointContainer.transform.localRotation = Quaternion.identity; // Restore the state of child rigidbody GameObjects now the anchor position has been set. for (int i = 0; i < rigidbodyChildren.Length; i++) { if (rigidbodyChildren[i].gameObject == jointContainer || rigidbodyChildren[i] == jointRigidbody) { continue; } rigidbodyChildren[i].gameObject.SetActive(childrenStates[i]); } }