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. Dismiss Notice

Question HOW DO PEOPLE MAKE FALLING SAND GAMES?!

Discussion in 'Scripting' started by nirmaljake, Jun 30, 2023.

  1. nirmaljake

    nirmaljake

    Joined:
    Jul 15, 2020
    Posts:
    58
    I have surfed across the internet, even used chatgpt, saw almost every singular youtube video any no one in the world shows any at least PIECE of code related to falling sand games they only showcase their game they either show the code for money (patreon) or they don't show the code at all, now I'm not saying I'm literally reliant on people showing code, but even without code there is no video for unity C# there is only one really good video about falling sand in C, nothing else, and even chatgpt Is so frustrating to work with it keeps thinking something else instead of the other, now i did not come here to rant I came here to say has anyone of ya'll made a falling sand game, if you did please, JUST PLEASE LINK THE PROJECT OR SMT, I'll at least learn the basics of the code so i can make my own falling sand game!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    I researched that for a while and I remember seeing good tutorials that explain it very well. Code isn't needed much because the principle is so universally applicable. You basically just update an array (of pixels) every frame, like this diagram shows:
    upload_2023-6-30_19-20-42.png

    Then there's plenty of example games out there in the open source, most of them not in C# but still, relatively straightforward to port.

    I would suggest starting with the very basic setup: a 2D array of colors (a texture) for instance, and then go over that array and for every color that isn't black move the pixel down a step, provided the pixel below it is "empty" (black). You could also use a 2D array of Cube meshes and do it in 3D. You will start to see pixels or cubes falling down, and then you can take it from there with the tips/demos from various tutorials.
     
    _geo__, Yoreki, Ryiah and 1 other person like this.
  3. nirmaljake

    nirmaljake

    Joined:
    Jul 15, 2020
    Posts:
    58
    Wow thanks for the info
    tbh that diagram is more helpful then the youtube videos I watched
    I'll be sure to try this technique out!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,559
    One name for this is cellular automata (CA).

    The most famous CA is probably Conway's Game Of Life.

    Attached is my Unity implementation.

    You could probably take the knowledge from @CodeSmile above and replace the
    CalculateOneGeneration()
    method in this project to make it be falling sand.
     

    Attached Files:

    Yoreki likes this.
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    btw in case you haven't noticed: that image links to the youtube video which explains the abstract concepts very well
     
  6. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,111
    Thanks for that. I tried to go to sleep but instead I have now spent an hour on youtube sinking ever deeper into that sandpit :D
     
    Lurking-Ninja, Ryiah and Kurt-Dekker like this.
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Yep... I know what I'm taking a crack at coding this weekend.
     
    Kurt-Dekker likes this.