Friday, April 26, 2019

unity - Destroy chunked block like Minecraft


I am trying to do a Minecraft-like game (like every one have already tried I guess) and i went across the problem i expected: lag with world Generation. I did lot of searching but i can


I tried to combine them into a bigger mesh, which work, but not i can destroy a single block. So how can I place/destroy chunked (mesh combined) block? If i cant, can someone clearly explain me a solution (i am not fluent in english nor with programming expressions, i can code tho) and how to do it (with a script example if possible :) ) ? Thanks in advance !


I generated my world using 2D perlin noise. Once i have the blocks, i split them into 16x16x100 chunk meshes.


Before Making chunks, i first tried to combine all of them into one block (using the unity manual), but the result is only one cube placed at 0,0,0. Where are all the other cubes ?


using System.Collections;

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ThisWorldGen : MonoBehaviour {

public GameObject blockPrefab;
public GameObject grass;
public GameObject dirt;
public GameObject rock;
private string[] blockTag;

private GameObject stockin;


public int amp;
public int freq;
private Vector3 mypos;
[SerializeField]
private Material material;
private Vector3 blockLocations;


public List meshes = new List();

void Start()
{
Generate();
}

void Generate()
{
amp = Random.Range(0,100);

freq = Random.Range(30,100);
mypos = this.transform.position;
int cols = 100;
int rows = 100;
float startTime = Time.realtimeSinceStartup;
#region Create Mesh Data
MeshFilter blockMeshes = Instantiate(blockPrefab, Vector3.zero, Quaternion.identity).GetComponent();

for(int x = 0; x < cols;x++)
{

for(int z = 0; z < rows;z++)
{
float y = Mathf.PerlinNoise ((mypos.x + x) / freq,(mypos.z + z)/freq) * amp;
y = Mathf.Floor(y);
for(float hy = y; y > 0.0F; y -= 1.0F)
{
blockMeshes.transform.position = new Vector3(mypos.x + x,y,mypos.z +z);//move the unit cube to the intended position
meshes.Add(blockMeshes);
}
}

}
int i = 0;
MeshFilter[] listMeshes = new MeshFilter[meshes.Count];
foreach(MeshFilter cubesToAdd in meshes)
{
listMeshes[i] = cubesToAdd;
i++;
}
CombineInstance[] combine = new CombineInstance[listMeshes.Length];
int w = 0;

while(w < listMeshes.Length)
{
combine[w].mesh = listMeshes[w].sharedMesh;
combine[w].transform = listMeshes[w].transform.localToWorldMatrix;
listMeshes[w].gameObject.SetActive(false);
w++;
}
Debug.Log (combine.Length);
transform.GetComponent().mesh = new Mesh();
transform.GetComponent().mesh.CombineMeshes(combine);

transform.gameObject.SetActive(true);
#endregion
Debug.Log("Loaded in " + (Time.realtimeSinceStartup - startTime) + " Seconds.");
}
}

(the world was generating randomly and perfectly before trying to merge the meshes)




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...