Hi all,
Very new to Unity and game development in general.
I’ve set up an isometric environment with players and enemies.
What would be the best method for creating a fixed sized terrain (player/user will choose the size that they want) with randomly generated objects and layout.
For example, a number of buildings of different types in random places, roads and floor textures of different types.
The terrain will always be flat.
I’ve searched the web but everything seems to come back with randomly generated infinite terrains.
Any advice welcome and appreciated.
Thanks
edit:
I have just found the script below on a website, I guess it would be a good place to start but I’m unsure where I should actually run it from. Should I attach the script to a Terrain GameObject?
using UnityEngine;
using System.Collections;
public class RandomObjects : MonoBehaviour
{
public Terrain terrain;
public int numberOfObjects; // number of objects to place
private int currentObjects; // number of placed objects
public GameObject objectToPlace; // GameObject to place
private int terrainWidth; // terrain size (x)
private int terrainLength; // terrain size (z)
private int terrainPosX; // terrain position x
private int terrainPosZ; // terrain position z
void Start()
{
// terrain size x
terrainWidth = (int)terrain.terrainData.size.x;
// terrain size z
terrainLength = (int)terrain.terrainData.size.z;
// terrain x position
terrainPosX = (int)terrain.transform.position.x;
// terrain z position
terrainPosZ = (int)terrain.transform.position.z;
}
// Update is called once per frame
void Update()
{
// generate objects
if(currentObjects <= numberOfObjects)
{
// generate random x position
int posx = Random.Range(terrainPosX, terrainPosX + terrainWidth);
// generate random z position
int posz = Random.Range(terrainPosZ, terrainPosZ + terrainLength);
// get the terrain height at the random position
float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
// create new gameObject on random position
GameObject newObject = (GameObject)Instantiate(objectToPlace, new Vector3(posx, posy, posz), Quaternion.identity);
currentObjects += 1;
}
if(currentObjects == numberOfObjects)
{
Debug.Log("Generate objects complete!");
}
}
}