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. Dismiss Notice

How Do I detect this connection?

Discussion in 'Scripting' started by CODE_BLUE77, Nov 13, 2014.

  1. CODE_BLUE77

    CODE_BLUE77

    Joined:
    Apr 1, 2014
    Posts:
    12


    As you can see from the image above. there are two machines and a pipe connecting them, the pipe is made of individual pieces as they can be placed by the user. I Have a problem where I cannot figure a way to make the two machines know they are connected.

    Lets say on the frist machine there is a function saying, if the pip it is touching is linked with the machine they can interact, but the problem is making all the pipes know if trhey are or not connect to what machines. Any ideas, Thanks
     
  2. CODE_BLUE77

    CODE_BLUE77

    Joined:
    Apr 1, 2014
    Posts:
    12
    bump, I Really need this to work :(
     
  3. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Just a suggestion, but why not try trigger colliders on the ends of the pipes?

    And maybe you could then have special triggers that let a pipe know it's connected to Machine A or B. Such a thing could be done like this: When the player places a pipe, have it parented to a gameobject of which the pipe segments are the children. For each pipe segment, a script on the parent object can then check that all pipes are connected to one of the other pipes. Then, if it also detects that two or more pipes have a machine on one end. (And all the pipes have a connection as well!) then it would register connected.

    The only issue is getting two different pipe networks to work, as depending on how this is implemented, it could end up that it's checking two sets of pipes instead. One way to combat this problem would be to create a new array for each set of pipes. that way, by checking the ends, (With OnTriggerStay, of course) if said array of pipes have all connected ends, (And two or more of the ends are attached to machines) then the machines are said to be connected. when that hapens, just have a boolean in your code to set when this happens. (Or not... :D)

    EDIT: Also, sorry for late reply, all this typing gets slow sometimes... :D (Funny part was, while I was typing, your post appeared and... :p)
     
  4. CODE_BLUE77

    CODE_BLUE77

    Joined:
    Apr 1, 2014
    Posts:
    12
    Thanks for the reply, im not entierly sure what you mean. But I have managed to get it to work one way, so if I connect asll the pipes together to both machines. it registers and works. but if I remove on of the pipes. i cannot think of a way to update all the other pipes to say its not connected. Thankyou though :)
     
  5. CODE_BLUE77

    CODE_BLUE77

    Joined:
    Apr 1, 2014
    Posts:
    12
    Im sorry to bump this but again I really need this to work
     
  6. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    The way to do it:
    I did forget one thing, and that is believe it or not, the removal of pipes! ;)

    if you're removing a pipe, the triggers should have a OnTriggerExit event that checks if the trigger that passed out of the collider (or the trigger itself was moved out) is another pipe, and if so, set the connected variable to false.

    Also, using onTriggerEnter to initially activate the variable will make it more stable. :)

    I also thought of another good way to make things smoother: why not a variable for checking either end, rather than both? :)

    EDIT: OOPS! Mean't good!
    :confused: :D
     
  7. CODE_BLUE77

    CODE_BLUE77

    Joined:
    Apr 1, 2014
    Posts:
    12
    Hmm well I will try and make what you said, I Will post it again soon to show you , and thanks again for the reply!
     
  8. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    You're welcome man! anytime. :)

    I have to go anyway, so... :D
     
  9. CODE_BLUE77

    CODE_BLUE77

    Joined:
    Apr 1, 2014
    Posts:
    12
    Do you have skype or something so I can talk toi you. I Seriously need help on this
     
  10. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Don't have skype, no. Sorry! :|

    But if I can think of an alternative (I am not much of a SM junkie, and not allowed to be anyway... :D) I will let you know. :)
     
  11. CODE_BLUE77

    CODE_BLUE77

    Joined:
    Apr 1, 2014
    Posts:
    12
    Okay well I will show you the code I have so far. It works one way, so if I link any pipes up and aslong as one is linked to the machine they call register as being connected to the "casher" aka machine. I Will show you the code but like I said it works one way. if i remove one of the pies they still thinkt hey are connected.


    #pragma strict


    public var isConnectedToCasher : boolean;


    function Start () {

    }

    function Update () {

    }

    function OnCollisionEnter(collision : Collision){

    if(collision.transform.tag=="Pipe"){

    if(collision.transform.GetComponent(PipeManager).isConnectedToCasher==true){

    isConnectedToCasher=true;

    }

    }

    if(collision.transform.tag=="Cookie Casher"){

    isConnectedToCasher=true;

    Debug.Log("Collided");

    }


    }

    function OnCollisionStay(collision : Collision){

    updatePipe(collision);


    }

    function OnCollisionExit(collision : Collision){

    if(collision.transform.tag=="Pipe"){



    }

    }

    //THIS IS TO STOP THE LAG OF GETTING COMPONENT LIKE 20 TIMES A FRAME XD

    function updatePipe(collision : Collision){

    if(collision.transform.tag=="Cookie Casher"){

    isConnectedToCasher=true;

    }

    if(collision.transform.tag=="Pipe"){

    if(collision.transform.gameObject.GetComponent(PipeManager).isConnectedToCasher==true){

    isConnectedToCasher=true;

    }else{

    isConnectedToCasher=false;

    }

    }

    yield WaitForSeconds(1);

    }
     
  12. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Ok, that looks complex! What I would have used would be something similar to this:
    Code (csharp):
    1. //this code assumes you use a triggerbox for each end of one pipe segment
    2. function OnTriggerEnter (info : Collider) {
    3. if (info.gameObject.CompareTag("PipeNozzleA")){
    4. connectedA = true;
    5. }
    6. }
    For the trigger tags, write A and B, then set them in your code accordingly. Have to go, so will give better explanation later