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

How to create a simple world generator in Unity3D?

Discussion in 'Scripting' started by danielkvist01, Jan 6, 2015.

  1. danielkvist01

    danielkvist01

    Joined:
    Sep 19, 2013
    Posts:
    2
    Hello!

    Info: This is a question from the Unity answer hub that I moved here because of suggestions from others.

    I'm creating a survival game, like minecraft, but instead of blocks, I have some simpler geometry, somehow similar to the geometry style in The Long Dark or Unturned. I'm going to create a simple world generator, but I don't really know how I can do it, so I have some questions:



    Creating and Saving

    • Can I create a new empty Scene from a C# script?
    • (If above is possible) Can I start to populate the new Scene, from a C# script?
    • (If above is possible) Can I serialize/save everything in the Scene, or just save the Scene as a .unity file, from a C# script?


    Generation


    I have thought of two ways of doing the generation. Either generate a Terrain, or to create a new flat (plane) Mesh and use the Diamond Square Algorithm to place some vertecies with random heights on it, to create a simpler terrain. I think the last one siuts best to the rest of my game's "geometry style", so thats the one that I want most, if it's possible, but I don't know (if I can) how I can do that from a C# script in unity.

    I also want to populate it with some trees, bushes, entities, structures etc. but I have an idea of how I can do that. I will just simply cast some raycast, and check if they hit the ground, and if they do, I'll randomly (with some kind of seeding) select one object and place it where the hit occurred. In that way, I can decide how many objects I want, by changeing the number of casted raycasts. I can also extend it so that I can controll how many of each object that will be spawned.



    Conclusion


    I want to, from a C# script, create a new Scene, generate a new flat mesh and use the Diamond Square Algorithm to place vertecies with random heights on it, so that it looks like a terrain, something like this, which demonstrates theDiamond Square Algorithm, and start to populate it with structures, foilage, entities etc. Lastly, I, also from a C# script, want to serialize/save the Scene, or all objects in the Scene, to a file.

    All help is greatly appreciated!

    // TheDDestroyer12
     
  2. arthur_fukushima

    arthur_fukushima

    Joined:
    Nov 12, 2014
    Posts:
    28
    Hi!

    To create dinamic worlds, you must study a lot of different topics.
    We can't give you the code to do this, but we can guide you to do this by your own, it's far better I think. = )
    I can recommend you some tutorials:

    Simple Procedural Mesh Generation:
    http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/

    Perlin Noise Video:


    Voxels tutorials (Awesome!)
    http://studentgamedev.blogspot.com.br/2013/11/tutorialdemo.html

    To save and load your terrains (or any data):
    http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer
    http://wiki.unity3d.com/index.php?title=Save_and_Load_from_XML

    Hope it can help you, good luck!
     
  3. danielkvist01

    danielkvist01

    Joined:
    Sep 19, 2013
    Posts:
    2
    Great video!

    But the problem is that I don't know how I can generate a new Mesh and add vertecies to it, and I don't know how to create a new Scene either, but thanks ;)