Home > Back-end >  A literal sandbox in Unity3D
A literal sandbox in Unity3D

Time:12-20

I'm new to Unity (and game development in general) and I've been trying to figure out how to make sand that will be affected by a gameobject. For instance, I'm trying to create a zen garden of sorts in the game where you can pull out various rakes and walk along the sand to make designs. I've done tons of searching but haven't quite been able to figure out where to start. I don't know if it'd be better to try and make it happen with heightmaps or a deformation mesh. I want the sand to ONLY be affected by the rake(s) when in use and not by the footprints of the character.

This may be a dumb question to ask so excuse my ignorance if so. No code provided because, well, I don't even know where to begin to achieve what I'm after (the only thing I've been able to find has been based on particle systems which I don't think is what I'm looking for). Any guidance would be huge for me because I'm feeling pretty low IQ right now

CodePudding user response:

Well, you don't start with the easiest project. Especially if you're new with Unity and maybe code. But hey maybe you'll be excellent ! So you'll need a shader for this. Here is a project that makes one for snow but the principle is the same for sand ;)

Good luck !

CodePudding user response:

For a start, you can use a heightmap but it would need to be rather granular so that smaller height differences are visible. If it is mostly the only thing to interact with, this should be working. I'd probably define the heightmap myself in a script to have more control on the details. Simple array of arrays (jagged array) like float[sizex][sizey] HeightValues a Mesh. The mesh is created in the script as well.

Then render it with DrawMeshNow (in Update()) or with a standard mesh renderer with your sand material (depends mostly on a good sand texture).

Furthermore, with some public methods on the script, you can then call to change the height values from another script (e.g. the one on the rake object when the user presses e.g. a mouse button). As soon as this happens, you update the attached mesh's vertices with the new values.

I am not 100% sure if the granularity of a mesh will be sufficient though, would need a bit of experimentation regarding it's resolution. A much more advanced (and involved) way would be to use a custom shader.

I like the zen garden idea by the way!

  • Related