- Home /
How does robocraft, a unity game handle damage
So im planning a game and for my idea i require to have a damage system like in robocraft.
Firstly, this is how it works.
You have a veicle that you construct with blocks and weapons ect that you can go into battle with other players using your robot.
When a block is hit multiple times and looses all its health is is destroyed (well i think it is deactivated) however i know how to do all that.
This is the part i need help with, once a certain block is destroyed is works out weather other blocks/weapons are still attached to your robot and destroys them if not
I just dont know how i can figure out weather other blocks are still attached to the main bot.
Any help is appreciated!
Answer by luxon001 · Oct 29, 2017 at 06:20 PM
Add a "main" tag and put it on the seat or whatever the main part is. then put a tag on each block "is connected". the tag will stay if the block is connected to the main component. Use very short range raycasts on each connectable side to determine wheter it is connected (if none of those hit an object tagged(is connected) then the block falls off). If 1 raycast hits the "main" component, then its tag will be "is connected", and if a raycast hits an object tagged "is connected" its tag will also be "is connected". The tags of all block objects will be set to none after an "is connected" object is destroyed/disabled. Then start an IEnumerator function that starts with a 0.1 second delay and will disable all blocks with the "none" tag. The rays take less than 0.1s to reconfigure all the tags and as such the function is only checking which objects are left out.
This should work but may end up being buggy if 2 bots are rammed ino each other, and will always be 0.1 seconds late but you could try to reduce the time.
Hope it helps... (i might have been awfully unclear)
Since this was the accepted answer, let me comment on it a bit:
I wouldn't use tags, because that's not really what they are meant for, and like the poster said, if two robots are really close to each other you'll have bugs.
Ins$$anonymous$$d you should use a component (ConnectionStatus or something like that) that has two public fields
public bool isConnected; // connection status
public GameObject Robot; // the robot to which this block is connected
Then when a block is destroyed, when you do your raycast you can do a GetComponent and check if the blocks hit by that raycast are connected and belong to the same robot
Answer by jeango · Oct 29, 2017 at 06:29 PM
You could have a hierarchy of blocks so that when you killed a block all its children are automatically destroyed with it.
If you don't want to use Unity's built in hierarchy (in the case for example of multiple parents, or of you don't want the transforms to be moved together) you could do something like this:
public class Block : MonoBehaviour {
public List<Block> children;
public List<Block> parents;
// use this to add child/parent relationship between objects
void AddChild(Block child) {
children.Add (child);
child.parents.Add (this);
}
// use this when your block's health drops to 0
void DestroyBlock() {
foreach (var item in children) {
item.RemoveParent (this);
}
Destroy (gameObject);
}
// destroys the block if he has no more parents
public void RemoveParent(Block parent) {
if (parents.Remove (parent)) {
if (parents.Count == 0)
DestroyBlock ();
}
}
}
that would only work if the objects were pipe-like and have only 2 connectable sides... Because with 6 sided cubes the hierarchy parent-child would be incredibly messed up and buggy...
you don’t necessarily have to use the built-in hierarchy. Each object could have an array of “children” blocks and when you destroy an object you just call destroy On all objects in the array which in turn will call destroy on their own children etc
Unless you mean that an object can have multiple parents.in that case I will also add an array of parent blocks To each block. So when a block is destroyed It informs his children then the children Remove that lock from their parents list and if after that the list is empty they destroy themselves
but each object will be connected to a bunch of other objects, meaning that each child object will be a "child" of other objects and it simply wouldnt work.
With the code I provided, whenever you add a block it will be connected to a certain number of already existing blocks (except the very first one, which would then be the "Core" of your robot). Just call AddChild on each of those connected blocks, and it should work like a charm.
ok i understand now that you published the code, makes sense.
If it works for you, don't hesitate to accept my answer, could help people with the same problem.
You may want to re-phrase your original question, however, because it's not really about damage.
Your answer