Search Unity

Suggestion about how to manage a brick like spaceship

Discussion in 'Scripting' started by Signo, Sep 26, 2014.

  1. Signo

    Signo

    Joined:
    Nov 22, 2012
    Posts:
    6
    Hello all, as a beginner I wish to have some steering direction about how to make stuff. I already made a "study project" to help understand how to make a gameobject composed of multyple colliders and have them communicate each other. Now I feel it's time to make a step up, and I wish to build a system that allow the player to build a spaceship with different "bricks" and then (obviously) blow stuff and explosions everywhere.
    So I bet there are a lot of ways to make things, and wish some input to resolve some issue I have:
    The spaceship will be a single rigidbody, with multiple colliders, everytime a collider is destroyed or damaged I need to "remanage" the ship resources, this is the easy part and I think within my reach. What I want to do, and don't know how, is to check "hull integrity", in other word, I want the ship to check if some brick (or a group of brick) remain isolated from the ship, in this case , this "debris" need to turn in a new rigidbody, that will float in space.
    Suggestions?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I suggest you take a look through the Unity Asset Store for some of the free complete projects, such as the space shooters.

    They can teach you a LOT, but you need to really make the effort to understand them. Fortunately, there are tons of forum posts asking questions about these games by others who came before you.

    Once you have an understanding of a functioning game, then you can begin to either modify that code to get closer to what you are trying to do, or else begin to form an idea how to accomplish your goals in your own project.

    There are many other Unity game demo samples available all over the net, as well as in these forums, and you can always start from those too.

    Kurt
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That sounds like an interesting problem (and a pretty advanced project). Let's see how you might tackle it. The key bit is when the ship takes damage and you "remanage" the ship resources.

    At that point, you need to determine which blocks are no longer attached to the "core" of the ship (whatever that is — I'd assume it is where the pilot sits). Moreover, if it's possible for entire hunks (composed of multiple blocks) to break off, you really want to find those hunks and build a combined rigidbody for them, too. Here's the standard algorithm for that sort of thing:
    1. Have a "int groupID" property on your blocks. Begin the algorithm by zipping through your list of blocks, assigning each block a unique groupID (just count up from 0 or whatever).
    2. Now set a local "renumbered any?" flag to false, then loop over your blocks again. For each one:
      1. Check whether the block is attached to some other block.
      2. If so, then Renumber the higher groupID to the lower one, where Renumber means:
        1. Find all blocks with the high groupID, and change them to the lower one.
      3. Also, if you renumbered any blocks, set the "renumbered any?" flag to true.
    3. Now, if "renumbered any?" is set, go back to step 2.
    So basically you start out with each block having a unique group ID, but wherever blocks touch, the lower group ID takes over. This process repeats until no more renumbering is done.

    At that point, you can easily see what blocks are part of what group, just by looking at the group IDs. Take each group and build a RigidBody with a bunch of Colliders in it, or whatever you need to do.
     
  4. Signo

    Signo

    Joined:
    Nov 22, 2012
    Posts:
    6
    Hi Joe, I think you nailed the problem square on.: ) So you suggest to create a list of all the "brick", and check integrity. That look cool, I was thinking about some form of array to manage all the vital informations that each brick contribute to.
    I will investigate the argument.
    Kurt I understand and really appreciate your suggestions, I will surely search for existing projest to learn to. But now I'm bugged by this idea that I wasn't able to sort out. For sure your suggestion will help me when I will have to create cannon turrets, weapons and all the fancy stuff that a battleship is supposed to have.
     
  5. Signo

    Signo

    Joined:
    Nov 22, 2012
    Posts:
    6
    Ok after some pause I managed to work on the system you described me, but I have one little issue:
    When a brick is destroyed, he send a message, and every brick check their neighbor to find if there is still a route to reach the "command module". The problem is that everything happen in the same loop, so not all brick receive the last updated value from their neighbors. This update should happen in a cascade effect way, propagating through the ship not in the same frame, but in a timespan that made sure that the updated information is correctly broadcast from brick to brick.
    Should I use a IEnumerator function to postphone the "update ship condition" in the assembly, or there is another way ?

    Thank you for your contributions.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Goodness no, don't reach for IEnumerator/yield in a case like this. :) Just do your damage-marking in Update (or some method called from that), and then check for integrity in LateUpdate (ditto).

    Or, you could just not worry about it — is anyone going to be able to tell if there is a one-frame delay between the last connecting block being destroyed, and the two halves starting to fall apart? I don't think so. These things have mass and inertia anyway.

    Explosion forces may alter that conclusion, but generally it's a good idea to spread your explosion forces over several frames anyway. So I still think it would be fine.

    Good luck!
     
  7. Deleted User

    Deleted User

    Guest

    How about Parent ? - Space ship.
    Each block be attached to it Parent .. then when you want to blow it up.. then just remove move it to a new transform parent and run a explode script on it and let the pieces go flying..
     
  8. Signo

    Signo

    Joined:
    Nov 22, 2012
    Posts:
    6
    Hi shawndg, all block are already children of a parent, but I want to be able to cut a ship in two or more pieces, when a brick explode, I need to know if the ship is still a single unit, or if is broken in two or more chunks. Joe, you're right, I have no hurry to determine the ship integrity, but I don't want to run the check more than once for each brick, because I would like to be able to have ships made of hundreds of parts. I need to decide if the info propagation should start from the command module, or from some other part, I think it should start from there.
    I think I should do something like this: when a brick explode, the command module change a value, this value is propagated to all contacting bricks. After this is done, I check if there are brick with a value different from the last issued value. If so, all those bricks are separated, and one of them if elected "command module"(so he will be parent of them all) of the new chunk, the integrity check is run again, to be sure that all those parts are connected, if not, another chunk is created.
    I'm perfectly willing to spread all those check in more than one frame, but I'm not sure how.
     
    Last edited: Oct 23, 2014