- Home /
The question is answered, right answer was accepted
Why Hinge Joint Component locks rigidbody transform?
when i use the code below, my rigidbody "myObj" requires a "Hinge Joint Component" attached to itself and then it locks on the scene and i can't control it with the control keys? Why this occurs? Please help... Note: I don't think it's because of this code, when i add Hinge Joint Component to myObj without using the script the same thing happens again. i think there must be something else that i miss. Here is the code:
using UnityEngine;
using System.Collections;
public class AttachControl : MonoBehaviour {
// Use this for initialization
void Start ()
{
} // Update is called once per frame
void Update ()
{
}
void OnCollisionStay(Collision collision) {
Transform myTarget = transform.Find("/myObj/myObjChild");
float px = myTarget.transform.position.x;
float py = myTarget.transform.position.y;
float pz = myTarget.transform.position.z;
GameObject attachedObj = GameObject.Find("attachedObj");
bool attachKey = Input.GetKey (KeyCode.F);
if (Input.GetButtonDown("attachKey")) {
attachedObj.rigidbody.position = new Vector3(px,py,pz);
hingeJoint.connectedBody = attachedObj.rigidbody;
hingeJoint.anchor = new Vector3(px,py,pz);
}
else if (Input.GetButtonDown("attachKey")) {
hingeJoint.connectedBody = null;
} } }
Answer by alee26 · Aug 26, 2012 at 11:46 AM
The solution has two case. First is, if you need a joint in a triggered condition else then collision, define the condition separately (like closing objects coordinates). Second, if you need the joint to be created after the first condition , use a script like "gameObject.AddComponent();".
Follow this Question
Related Questions
Hinge Joints, distance to connected body 1 Answer
How can i change HingeJoint's axis from code? 0 Answers
Hinge Joint? Unconstrained. 1 Answer
"Gluing" pieces together for a breakable object with springs 1 Answer
Joints vs Mass 0 Answers