- Home /
Place block script
I had put the break/place block script in my engine and when i right clicked for placing a block i got a huge gaping hole in the ground, breaking blocks with the left click works fine, Can i get a little help? This is the code..
<p>var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;</p>
<p>function Update () {
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
}</p>
<p>function Build() {
if (HitBlock()) {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.transform.position + hit.normal;
}
}</p>
<p>function Erase() {
if (HitBlock())
Destroy(hit.transform.gameObject);
}</p>
<p>function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}</p>
$$anonymous$$y bad, I've never put a code in a post before (:
Answer by Dreamer · May 11, 2011 at 08:42 AM
You just select all the codes, and press the "101" button on top. I help you organize the codes first, then I take a look at it.
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); }
O$$anonymous$$, in this script. Left Click is to create a Cube object and Right click is to destroy the object you click on !
Oh, Haha I feel so silly now.. Okay tell me when your ready.
Well the left click was breaking blocks and the right click was breaking blocks but with a bigger size, Lol.. It may be the other scripts colliding with it or something.. I've been looking for a Break/Build script for awhile now.
i want to do the same sort of thing put to place and destroy where the mouse is not where the camera is looking could you please help change it to do that please?
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Here at Unity Answers, Answer means Solution, not Response.
watch this video : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
read this page : http://answers.unity3d.com/page/faq.html