Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity3d Need help to modify this scipt.

Discussion in 'Scripting' started by Claymore, Jan 27, 2013.

  1. Claymore

    Claymore

    Joined:
    Oct 17, 2012
    Posts:
    158
    Hello,this script makes objects stick to any collision....When game starts it creates connection between collision and if i press "n" it breaks that connection and again when i touch collision (same thing.....) Well what i want to do is : Only when i press "n" THEN it creates connection and i have to walk up to any collision and its gonna stick to that,not at game start but only when i press "n" it makes connection and when i press "n" again it breaks connection.I were messing up with this and i think its impossible :? So,do you have any ideas? Help?

    Script:
    Code (csharp):
    1. var jointExist : boolean;
    2.  
    3. function OnCollisionEnter(c : Collision) {
    4.     if(!jointExist){
    5.       var joint = gameObject.AddComponent(FixedJoint);
    6.       joint.connectedBody = c.rigidbody;
    7.       jointExist = true;
    8.    }
    9. }
    10.  
    11. function Update(){
    12.    if(Input.GetKeyDown(KeyCode.N)){
    13.       var joint = gameObject.GetComponent(FixedJoint);
    14.       Destroy(joint);
    15.       jointExist = false;
    16.    }
    17. }
     
  2. Claymore

    Claymore

    Joined:
    Oct 17, 2012
    Posts:
    158
  3. Elryk

    Elryk

    Joined:
    Jan 2, 2013
    Posts:
    82
    Your post was a little hard to understand, but I'm going to take a shot at this anyways and hope I correctly understand what your wanting to do.

    If you don't want it to make a joint unless 'n' is pressed, you could try copying the if statement on line 12 and put it just above, just below, or mixed (using between your conditionals) with line 4.
     
  4. Claymore

    Claymore

    Joined:
    Oct 17, 2012
    Posts:
    158
    Yeah,i tried everything,same that way,i just need example or modification of my script,that would be awesome,cause my nerves are weak :D :D
     
  5. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,249
    put all the code in oncollisionenter inside an if(Input.GetKey(KeyCode.N)) (or whatever unity uses to see if a key is being pushed. Don't use GetKeyDown here or it will only work on the update you push the key down.

    However be warned you might like to pick different keys for join and unjoin.
     
  6. Claymore

    Claymore

    Joined:
    Oct 17, 2012
    Posts:
    158
    I cant just put that script there,i need alot of modification,i dont understand what an where and again what to modify........