- Home /
Realistic block placing/building
I want to make a script that has block placing. It shows you where you are going to place the block. You click to place and right click to destroy. How would I make a script for this?
Well first, you'll have to create a script called BlockEditor or something along those lines. Whatever you fancy can be the script name. Then I suppose you'll want to raycast from the mouse pointer to the 3D or 2D game space or the boxes (depending on left click or right click). This might help with that part of the script: http://answers.unity3d.com/questions/13577/using-raycast-to-get-mouse-input.html I didn't read much into it but you can search other answers for something more useful. So, after if its a left click you have the position of the 3d space you'll make the gameobject with that position. For right mouse you'll just destroy the box you hit.
You code should/might look like something along these lines
void On$$anonymous$$ouseDown() {
leftClick = check$$anonymous$$ouseClick();
if(leftClick)
//raycast logic here
//test against your plane or whatever you ground is bascially
//when you have your Vector2D/3D position do something like
createBox(position);
else
//raycast logic here
//test against your boxes to see if hit one
//when you have hit a box
destoryBox(box);
}
bool check$$anonymous$$ouseClick() {
//do some sort of check if it was left
// or right mouse down
//not sure how to do that so good luck on that one
//return true if left click
//return false if right click
}
void destoryBox(Box toBeDestoryed) {
Destroy(box);
}
void createBox(Vector position) {
//then something along these lines not exactly this
Instantiate (box, position, Quaternion.Identity);
}
Answer by sxriter · Feb 01, 2013 at 10:31 PM
What you need exactly? Crosshair or Place/destroy blocks?
Your answer
![](https://koobas.hobune.stream/wayback/20220613085848im_/https://answers.unity.com/themes/thub/images/avi.jpg)