script terrain cubes (I nned help.)

I need help scripting a script in any type to make so that the terrain when the game starts it creates cubes across the terrain that is level to the terrains leve by increments of one. i dont want to do it by hand because it takes too long to place 2000 blocks without a render because i want it to follow the terrain as it it is like minecraft. so when i do a raycast instantiate script it will attatch to the terrain. If you can do so ill be so hapyy.:slight_smile:
as i not a excelent programmer, i am a okay programmer.

I neeed the script to add cubes ot the terrain every cubic so that it conforms to the terrains hieght changes and level so that they create a cuby landscape so that the cubes coevr the landscape and conform to it.

What part of such a script is giving you difficulty?

Basically you want to do something like a double loop for the x and z axis incrementing the loop by the size of your cube and then sample the terrain height at the center of your square. Once you’ve got the terrain height (y) and the looped x,y you can form a vector3 position to instantiate your cube.

similar to that btu i want ti to instantiate cubes acrross the whole map without the renderer so that you dotn see them. and i want them to follow the ups and down of the terrain. im tryign to use samplehieght but one i cant put in cordintes for the whole map and 2 i cant put them on public so the instantiate will call from it

i am going to have to leave by 3 so after that i might not beable to get abck until then

I’m missing something here. Do you want 1 cube for the whole terrain or a bunch of cubes to cover the terrain? Also what’s this bit about them moving up and down? If you’ve looped through the terrain x,z values finding the given height at that point then the cube when you instantiate will be at the terrain height.

Also if the source prefab that you instantiate doesn’t have a render component on it then of course the instantiated objects won’t have renderer’s either and won’t be visible.

I want ti to go up and down to follow the terrain both verticlaly and horizontally. i want the cubes to cover it

hello anyoen there

Give this a shot.
You’ll need to provide a game object prefab of the cube that you want to represent a block to copy onto the terrain.

Be careful here though there could be a LOT of blocks if you have small blocks and / or a large map. Set your test map size to something reasonable like 500x500 and a cube size of 5 for a quick test to see how it works.

Drop the below script onto your terrain object and set the block prefab and block size values.
Press Run

#pragma strict

//this is the object representing a block that will be copied onto the terrain
public var TerrainCube : GameObject; 

/*this is a representation of the size of a block without having to code a size
lookup function */
public var blockSize : Vector3;

function Start () 
	{
	var TerrainToCover : Terrain = GetComponent(Terrain);
	CoverTerrainWithBlocks(TerrainToCover, TerrainCube, blockSize);	
	}
	
function CoverTerrainWithBlocks(terrainToCover : Terrain, blockObject : GameObject, blockSize : Vector3)
	{	
	var TerrainToCover : Terrain = GetComponent(Terrain);
	
	var maxBlockXaxis : int = Mathf.CeilToInt(terrainToCover.terrainData.size.x / blockSize.x);
	var maxBlockZaxis : int = Mathf.CeilToInt(terrainToCover.terrainData.size.z / blockSize.z);

	var blockOffsetX : float = blockSize.x/2;
	var blockOffsetY : float = blockSize.y/2;
	var blockOffsetZ : float = blockSize.z/2;
	
	var checkBlockPosition : Vector3;
	var yieldBlockCount : int = 0;
	
	for (var terrainX : int = 0; terrainX < maxBlockXaxis; terrainX++)
		{
		for (var terrainZ : int = 0; terrainZ < maxBlockZaxis ;terrainZ ++)
			{
			checkBlockPosition.x = terrainX * blockSize.x + blockOffsetX;
			checkBlockPosition.y = terrainToCover.SampleHeight(checkBlockPosition)+blockOffsetY;
			checkBlockPosition.z = terrainZ * blockSize.z + blockOffsetZ;
			//possible height check point			
			
			Instantiate(blockObject,checkBlockPosition,Quaternion.identity);
			
			//after creating 50 objects pass yield execution until next frame;
			yieldBlockCount++;
			if(yieldBlockCount > 50)
				{
				yieldBlockCount = 0;
				yield 0;
				}
			}
		}	
	}

THat relaly helped thanks much :smile: cause im a girl im nto good a programming thanks im goign to mod the script to where it graphs multple texture to create biomes

I didn’t realize gender had anything to do with programming ability. Someone should tell all the terrible male programmers I’ve come across to step up their game.