Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question In-game Building system -> How to prevent "stacking" built assets?

Discussion in 'Scripting' started by LambdaTheDev, Jan 14, 2021.

  1. LambdaTheDev

    LambdaTheDev

    Joined:
    Jan 15, 2020
    Posts:
    53
    Hello guys,
    Sorry for bad title, but I have no idea how to form it properly.

    I am making a Fortnite-alike building system in my game, and I have a question, how can I prevent the situation shown in the attachment.

    Info about building assets:
    - Y-axis is always the same
    - Each asset is a square of same size

    Also, I hope you understand what I mean on the image. If any questions, ask me.

     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    If you have a grid you have to quantify positions to that grid.

    When you have a grid, you keep track of what is in each cell using some kind of collection, such as:

    - a Dictionary with a positional key

    - a 2-dimensional array.
     
  3. LambdaTheDev

    LambdaTheDev

    Joined:
    Jan 15, 2020
    Posts:
    53
    Well, I forgot to add, that it is 3D game and my terrain can be infinite.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Then you would need a Dictionary with a positional key rather than a 2-dimensional array.

    Quantification still applies. That applies to any grid game, 1D, 2D, 3D or even 4D if that's possible.
     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    If you're looking to snap these items together like in fornite then there's a specific technique you can use involving putting empty gameobjects that act like nodes onto each side of your gameobject, this same technique I think could be used in 2D.



    Be warned though it is a fairly deep topic you're asking about.
     
    Kurt-Dekker likes this.
  6. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    For the game I created not long ago, I did a socket design very similar to what's posted by @Lethn above.

    A building block would have a bunch of empty child gameobjects which would act as a socket. The sockets would be positioned prior to run time.

    Each socket had a script on it that contained a list of buildingblocks that it would accept.

    I then just used a raycast from the players camera forward position to check whether sockets were available or not and would place a preview block if so.
     
    PraetorBlue, Lethn and Kurt-Dekker like this.