Hello, I have been working on a minecraft game for quite a while now and I am having some problems with creating Bedrock (Undestroyable block) for the bottom and edges of my world. This is my script:
using UnityEngine;
using System.Collections;
public class BuildScript : MonoBehaviour {
RaycastHit hit;
public Transform prefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
Vector3 G = new Vector3(Mathf.Round(hit.point.x), Mathf.Ceil(hit.point.y), Mathf.Round(hit.point.z));
if(Physics.Raycast(ray, out hit)){
if(Input.GetMouseButtonDown(1)){
Destroy(hit.collider.gameObject);
}
if(Input.GetKeyDown("q")){
Instantiate(prefab, G, Quaternion.identity);
}
}
}
}
It works perfectly exept it is Destroy(hit.collider.gameObject); and it destroys any object, I would like the bedrock to be undestroyable. But everything else is, Please help me.
P.S. I also need help with the terrain, I need a random terrain of different heights made of cubes. This would be a massive help, Thankyou.
You didn’t describe the logic you employed in your script but if I understand it correctly, you are casting a ray and, if the ray hits something AND the mouse button is down then delete the game object that was hit by the ray. If you want to “not” delete some game object in this story then you will need to provide some “marker” on that game object to flag it as undeletable. For example, perhaps define a new game tag type called “undeletable” and flag these game objects as having this tag. Then in your code you could have the following:
if (Input.GetMouseButtonDown(1) && hit.collider.gameObject.tag != "undeleteable")
Using an attached tag value is one way of detecting marked game objects. Another would be attached scripts with properties.
Obviously you’d want to set the bedrock object(s) to have the bedrock tag.
For the random terrain…
Start at a random height, build cubes from bedrock to [height].
For each terrain position, choose a height close to an adjacent height, then build cubes from bedrock to [height].
Note -
This is really really inefficient. Storing that many cubes will explode the computer. There are ten billion questions on here about minecraft, though, and some of them even have helpful answers.