- Home /
Question by
DenilsonRF · Nov 07, 2021 at 06:57 PM ·
script.scripting beginnerscriptingbasicsworldchunk
How do I improve my world generating script?
Hello everyone, I'm working on a game and new to programming. I'm trying to make a world generating code but I'm really struggling. So far I was able to just make a plane of grass blocks with a few landscape objects (tree and rock) randomly spawning on top of it.
This is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class worldbuild : MonoBehaviour
{
public Transform grassBLK01;
public Transform treeOBJ01;
public Transform rockOBJ01;
private int spawnchance;
void Start()
{
for (float xPos = -50; xPos < 50; xPos += 4)
{
for (float zPos = 0; zPos < 50; zPos += 4)
{
Instantiate(grassBLK01, new Vector3(xPos, 0, zPos), grassBLK01.rotation);
spawnchance = Random.Range(1, 80);
if (spawnchance > 1 && spawnchance < 3)
{
Instantiate(treeOBJ01, new Vector3(xPos, 4, zPos), treeOBJ01.rotation);
}
if (spawnchance == 10)
{
Instantiate(rockOBJ01, new Vector3(xPos, 4, zPos), rockOBJ01.rotation);
}
}
}
}
void Update()
{
}
}
I"m trying to improve the world generating. How do I make it count as one object instead of spawning 1000 grass prefabs? And how do I multiply it so I can choose how many chunks it generates side by side?
I'm really new to programming, usually I stick to storytelling, visual concepts and modeling but decided to try making this personal project of mine all by myself and man am I struggling on the programming.
capturar.png
(512.5 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613045108im_/https://answers.unity.com/themes/thub/images/avi.jpg)