Search Unity

How would i go about attaching multiple objects at runtime?

Discussion in 'Editor & General Support' started by MastaPlatypus, May 14, 2020.

  1. MastaPlatypus

    MastaPlatypus

    Joined:
    Apr 16, 2019
    Posts:
    4
    heyo!
    i'm recently trying my hand with unity by making a game about space stations and i have a question:
    as the title says the objective is to give the player the ability to build the stations piece by piece, in a modular way, but i'm not sure how should i approach the "docking" procedure.
    i would like each station module to be able to be attached to eachother by childing itself to the module it's being docked to and my first idea was to add a trigger collider to each docking port so that when they get under a specific distance this would happen.
    my problem with this tho is that if i make an universal "docking port" prefab when two get into contact they would both try to child themselves to one another and im guessing that would be trouble, making two separate prefabs (like a "master" and "slave" port) woudnt work either because i cant say beforehand wich orientation/side a player will be trying to to dock them.
    do you guys have any suggestion on how shoud i try to do this? thanks in advance for the help!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I wouldn't suggest doing it this way because at some point you'll probably want a piece of the station to dock to two different pieces at the same time (maybe you add a tunnel between two station pieces). But a GameObject cannot be a child of two different parents at the same time.

    I might just make a root station object, and all stations pieces become child objects of the root station object. The relationships between the station pieces are then done in code, as component scripts you attach to the station pieces, with references to the other station pieces they are connected to. This would be a more flexible system most likely.
     
  3. MastaPlatypus

    MastaPlatypus

    Joined:
    Apr 16, 2019
    Posts:
    4
    this is a great idea, i think i'll take this route.
    i can think of a few edge cases (like if a player tries to dock two modules when neither of those is or is connected to the "root" module) but i could simply make it that the only way to connect two modules is if one of them is part of the root "family" and impossible othewise. thanks for the help!
     
    Joe-Censored likes this.