Hello Guys,
I am trying to follow this thread a bit and you guys have been a great help. I am at the moment developing a marching cubes terrain. I started off with a normal Voxel Minecraft terrain, which worked out perfecly well. It is hard for me to read through the entire thread, so I thought I would post my problem: My terrain engine is supposed to help edit chunks, which worked perfectly well on my blocky version of it. Although now if I click often nothing happens and the chunks appear to be edited only sometimes(only if the colliders have a certain shape). I’ve tried everything I could think of. Subtracting the hit.normal didnt work. Tell me if you need more scripts.
using UnityEngine;
using System.Collections;
public class PlayerIO : MonoBehaviour {
public int viewRange;
public float intRange;
public Chunk ChunkPrefab;
public static int width {
get { return World.thisWorld.chunkWidth; }
}
public static int height {
get { return World.thisWorld.chunkHeight; }
}
void Start() {
if(viewRange == 0) viewRange = 30;
StartCoroutine(UpdatePos1());
StartCoroutine(UpdatePos2());
}
void Update() {
if(Input.GetMouseButtonDown(0)) {
StartCoroutine(RemoveBlock());
}
}
IEnumerator RemoveBlock() {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit, intRange)) {
Vector3 p = hit.point;
Chunk chunk = Chunk.FindChunk(p);
if(chunk == null) return false;
float x=p.x-chunk.transform.position.x, y =p.y,z =p.z-chunk.transform.position.z;
int a = Mathf.FloorToInt(x), b = Mathf.FloorToInt(y), c = Mathf.FloorToInt(z);
if(x < width -1 && z < width -1) {
chunk.data[a,b,c]=0f;
chunk.data[a+1,b,c]=0f;
chunk.data[a,b,c+1]=0f;
chunk.data[a+1,b,c+1]=0f;
}else if(x == width - 1 && z == width -1 ) {
chunk.data[a,b,c]=0f;
Chunk chunk1 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x+2,y+chunk.transform.position.y,z+chunk.transform.position.z+2));
chunk1.data[a-width,b,c-width] = 0f;
StartCoroutine(chunk1.UpdateAt());
Chunk chunk2 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x+2,y+chunk.transform.position.y,z+chunk.transform.position.z));
chunk2.data[a-width,b,c]=0f;
StartCoroutine(chunk2.UpdateAt());
Chunk chunk3 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x,y+chunk.transform.position.y,z+chunk.transform.position.z + 2));
chunk3.data[a,b,c-width] = 0f;
StartCoroutine(chunk3.UpdateAt());
StartCoroutine(chunk.UpdateAt());
}else if(x == width - 1 ) {
chunk.data[a,b,c]=0f;
chunk.data[a,b,c+1]=0f;
Chunk chunk1 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x+2,y+chunk.transform.position.y,z+chunk.transform.position.z));
chunk.data[a-width,b,c]=0f;
chunk.data[a-width,b,c+1]=0f;
StartCoroutine(chunk1.UpdateAt());
}else if(z == width - 1 ) {
chunk.data[a,b,c]=0f;
chunk.data[a+1,b,c]=0f;
Chunk chunk1 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x,y+chunk.transform.position.y,z+chunk.transform.position.z+2));
chunk.data[a,b,c-width]=0f;
chunk.data[a+1,b,c-width]=0f;
StartCoroutine(chunk1.UpdateAt());
}
StartCoroutine(chunk.UpdateAt());
if(x == width -1 || z == width -1) {
StartCoroutine(chunk.UpdateAt());
}
if(x <= 1) {
Chunk atchunk = Chunk.FindChunk(hit.point - new Vector3(2,0,0));
StartCoroutine(atchunk.UpdateAt());
}
if(z <= 1){
Chunk atchunk = Chunk.FindChunk(hit.point - new Vector3(0,0,2));
StartCoroutine(atchunk.UpdateAt());
}
if(z <= 1 && x <=1){
Chunk atchunk = Chunk.FindChunk(hit.point - new Vector3(2,0,2));
StartCoroutine(atchunk.UpdateAt());
}
}
yield return 0;
}
IEnumerator UpdatePos1 () {
while(viewRange > 0){
for(float x = transform.position.x - viewRange;x < transform.position.x + viewRange; x += width ) {
for(float z = transform.position.z - viewRange;z < transform.position.z + viewRange; z += width ) {
Vector3 pos = new Vector3(x,0,z);
pos.x = Mathf.Floor(pos.x / (float)width) * width;
pos.z = Mathf.Floor(pos.z / (float)width) * width;
Vector3 delta = pos - transform.position;
delta.y = 0;
if(delta.magnitude > viewRange) continue;
Chunk chunk = Chunk.FindChunk(pos);
if(chunk != null) continue;
chunk = (Chunk)Instantiate(ChunkPrefab, pos, Quaternion.identity);
yield return new WaitForEndOfFrame();
}
}
yield return new WaitForSeconds(0.5f);
}
yield return 0;
}
IEnumerator UpdatePos2 () {
while(viewRange > 0){
for (int a = 0; a < Chunk.chunks.Count; a++) {
Vector3 pos = Chunk.chunks[a].transform.position;
Vector3 delta = pos - transform.position;
delta.y = 0;
if(delta.magnitude < viewRange) continue;
Destroy(Chunk.chunks[a].gameObject);
yield return new WaitForEndOfFrame();
}
yield return new WaitForSeconds(3);
}
yield return 0;
}
}
