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
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… )
EDIT: Also, sorry for late reply, all this typing gets slow sometimes… (Funny part was, while I was typing, your post appeared and… :p)
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
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?
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”){
} 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”){
Ok, that looks complex! What I would have used would be something similar to this:
//this code assumes you use a triggerbox for each end of one pipe segment
function OnTriggerEnter (info : Collider) {
if (info.gameObject.CompareTag("PipeNozzleA")){
connectedA = true;
}
}
For the trigger tags, write A and B, then set them in your code accordingly. Have to go, so will give better explanation later