Search Unity

Player built

Discussion in 'Scripting' started by Strikewyrm, May 27, 2019.

  1. Strikewyrm

    Strikewyrm

    Joined:
    May 27, 2019
    Posts:
    15
    So I've looked around abit but havent found the answer, how would you handle a player building a vehicle out of predefined pieces such as in cross out or robocraft (there are more but those are the two that stuck).

    Edit: just to clarify my skill level this is my first major project and I'm going to be doing it more or less solo.
     
    Last edited: May 28, 2019
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Depends on how you want the game to work. You can have each block define its own behaviours, so that adding a thruster block for example, propels the object in a certain direction no matter what... or you could have some sort of blueprint object, that always works roughly the same, and then the player is just getting creative with the overall shape.
     
  3. Strikewyrm

    Strikewyrm

    Joined:
    May 27, 2019
    Posts:
    15
    Combination of the two: basic blocks that the majority is built from and then "function" blocks that are attached such as locomotion and weapons
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    So which aspects are you struggling with? Placing blocks next to each other is obviously going to be step number one. Adding functionality is fairly straight forward, register the block when it's placed with some sort of controller for the overall object so that it can handle the data bookkeeping.
     
  5. Strikewyrm

    Strikewyrm

    Joined:
    May 27, 2019
    Posts:
    15
    I guess the issue is around the overall controller and how to store the data.
    As a side note this is going to be my first major project.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    My first instinct was that this was too hard for a first project, but then remembered that my first real Unity game back in 2006 had almost this exact same concept, so, maybe not.

    Storing the data will be the biggest challenge, and one that I did pretty badly when I tried. I recommend watching this video to get a handle on how proper save/load functionality works.

    As for building, you're going to have to become intimately familiar with the physics engine. Determine an object's location by raycasting: Create a dummy object with no physics that you set to the raycast's hit.point, and orient based on the raycast's hit.normal.

    When the user clicks, create the object at that position and attach it to the object the racyast hit with a physics joint. In this sort of game generally every object will have only one "parent", so overall it will form a sort of tree, with one root part that everything ultimately attaches to.

    For storing (make sure you've seen the above linked video for context), each object will need a unique ID (can just be a number that you increment each time you create one), a string representing its prefab name (which you will load via Resources.Load), position, rotation, and parent object ID. When loading, you will need to connect these objects to their parents by finding them based on that ID.

    Another challenge will be optimization, so start getting familiar with the Profiler window. It'll be easier if you start getting a handle on what's "normal" before adding a bunch of stuff, so even just having the window open and running in your peripheral vision from the start will be helpful.
     
  7. Strikewyrm

    Strikewyrm

    Joined:
    May 27, 2019
    Posts:
    15
    Still not sure I fully understand so Gona use a metaphor to see if I have it right: it's like a bead sculpture, where the beads are the individual pieces and the wire they are on is the parent structure, you save the data on what (in a particular level) has been destroyed to the parent structure (possibly with a simple if yes then don't render that piece)?