How to destroy a fixed joint through script controlled by key press

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?

OK I worked it out. I put a separate script onto the ‘Container’ object that is being picked up that deletes the FixedJoint

function Update () {

var Container = gameObject.GetComponent(FixedJoint);
    if (Input.GetKey("joystick button 7")){
        Destroy (Container);
}
}
3 Likes

I works for me also. Thanks for this answer.

Hi guys, I’m new to developement in unity. I’m trying to do a crane simulator but i have no clue where to start !!!
Have you developed your own 3D model for crane?
Can you guys help me out?

Hi srikar8!
Building a realistic crane simulator is not an easy job, as I’m learning myself.

You can start from the Docs looking for Hinge Joints and Fixed Joints descriptions.
There are also some generic tutorials on youtube that helps you understand the logic with which those joints works.
If you don’t mind building your ropes from the editor, it’s a bit easier!

If your goal is to put some ropes in a videogame for your player to grab and jump around with them, many programmers suggest to fake them instead of building them with physics, as they are very expensive in term of resources and performances.

Here are some links to get you started: