I have a crane simulator that I am building. I have pretty much everything working as I would except the pickup and drop feature.
I was originally doing it with parenting but this threw up a ton of issues due to known problems when parenting a rigidbody to another game object. I decided to try using a collision and a fixed joint which is working great except from the fact that I cannot work out how to ‘destroy’ the fixed joint. I need this to be destroyed only when the player presses a specific button or joystick key. I thought it might be something like the code I have at the bottom of the script but that does not work at all.
//javascript
function OnCollisionEnter(myCollision: Collision) { //find the object that the hook collides into
if(myCollision.gameObject.name == "kontajner_001"){ //test to see if the collision object is called 'Container'
if(!myCollision.gameObject.fixedjoint){ //make sure there is not already a fixed joint
if(!myCollision.gameObject.rigidbody) //make sure there is not already a rigidbody
myCollision.gameObject.AddComponent(Rigidbody); //add rigidbody to the 'Container' object
myCollision.rigidbody.mass = 200; // set the rigidbody mass to 200
var fixedJoint: FixedJoint = myCollision.gameObject.AddComponent(FixedJoint); //add fixed joint to 'Container'
fixedJoint.connectedBody = GameObject.Find("Big Hook").rigidbody; //connect the fixed joint to the crane hook
}
}
}
//Below is the code I am uncertain of - how to destroy an existing 'FixedJoint' on a button press so that the object returns to the control of gravity
/*
function Update () {
var Container : GameObject = GameObject.Find ("kontajner_001");
if (Input.GetKeyDown(KeyCode.W))// GetComponent (Container.FixedJoint)
Destroy (GetComponent (Container.FixedJoint));
}
*/
Any ideas how to accomplish this?