As I am a noob to Unity I need some help.
I tried to make a game with block placing/erasing and used this script:
var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
function Update () {
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
}
function Build() {
if (HitBlock()) {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.transform.position + hit.normal;
}
}
function Erase() {
if (HitBlock())
Destroy(hit.transform.gameObject);
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}
The problem is that the script is also destroying my terrain and any other gameobject.
I want to somehow detect if the object that is hit by the ray is a cube that is allowed to be destroyed or a object that shouldn’t be destroyed.