Okay I am trying to make this voxel based game (Not a minecraft clone its a fast-paced combat game). Though I just want to add this small part to the game which is breaking and
placing cubes. Yep! That works fine but when I look down at my feet and I right-click (Break) I can destroy my player also I have to move far back to place a block or it will
place it where my player is.
All I have is a camera (Break script is here) and a pickaxe parented to the camera so it
looks like I’m breaking blocks with a tool.
Heres my break script:
#pragma strict
var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
function Update () {
if (Input.GetMouseButtonDown(1)) Build();
if (Input.GetMouseButtonDown(0)) 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);
}
Thanks in advance!