Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Editor & General Support' started by Chani, Aug 26, 2011.

  1. Chani

    Chani

    Joined:
    Aug 25, 2011
    Posts:
    8
    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.

    Code (csharp):
    1.  
    2. //javascript
    3.  
    4. function OnCollisionEnter(myCollision: Collision) {  //find the object that the hook collides into
    5.  
    6.  if(myCollision.gameObject.name == "kontajner_001"){ //test to see if the collision object is called 'Container'
    7.   if(!myCollision.gameObject.fixedjoint){ //make sure there is not already a fixed joint
    8.   if(!myCollision.gameObject.rigidbody)  //make sure there is not already a rigidbody
    9.      myCollision.gameObject.AddComponent(Rigidbody); //add rigidbody to the 'Container' object
    10.      myCollision.rigidbody.mass = 200; // set the rigidbody mass to 200
    11.  
    12.    var fixedJoint: FixedJoint = myCollision.gameObject.AddComponent(FixedJoint); //add fixed joint to 'Container'
    13.    fixedJoint.connectedBody = GameObject.Find("Big Hook").rigidbody; //connect the fixed joint to the crane hook
    14.         }
    15.     }
    16. }
    17.  
    18.  
    19.  
    20. //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
    21.  
    22.  
    23.  
    24. /*
    25. function Update () {
    26. var Container : GameObject = GameObject.Find ("kontajner_001");
    27.     if (Input.GetKeyDown(KeyCode.W))// GetComponent (Container.FixedJoint)
    28.         Destroy (GetComponent (Container.FixedJoint));
    29. }
    30. */
    31.  
    Any ideas how to accomplish this?
     
  2. Chani

    Chani

    Joined:
    Aug 25, 2011
    Posts:
    8
    OK I worked it out. I put a separate script onto the 'Container' object that is being picked up that deletes the FixedJoint

    Code (csharp):
    1.  
    2. function Update () {
    3.  
    4. var Container = gameObject.GetComponent(FixedJoint);
    5.     if (Input.GetKey("joystick button 7")){
    6.         Destroy (Container);
    7. }
    8. }
    9.  
     
    mateuslucch, fateshow and Prances like this.
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,042
    I works for me also. Thanks for this answer.
     
  4. srikar8

    srikar8

    Joined:
    Oct 15, 2014
    Posts:
    1
    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?
     
  5. esitoinatteso

    esitoinatteso

    Joined:
    Sep 23, 2013
    Posts:
    26
    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:


    http://docs.unity3d.com/Manual/class-HingeJoint.html