- Home /
Question by
Lamprey · Jul 19, 2015 at 01:14 AM ·
c#instantiatebuildingonmousedragconfirm
How to implement building placement?
So I'm working on a management game's building system. I have the preview of what you'll build and single building placement down 100%, but I'm having trouble making a system where you drag out blocks and then when you're done, it gives you the option to either confirm and build your preview, or if you decline, to delete the preview. I am very frusturated and I will post my "attempt" down below:
public void GetBuildArea ()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 v;
v.x = selector.currentObject.transform.position.x;
v.y = 0;
v.z = selector.currentObject.transform.position.z;
ZoneStart = v;
}
if (Input.GetMouseButton(0))
{
Vector3 v = selector.currentObject.transform.position;
ZoneEnd = v;
RenderPreviewArea();
}
}
public void RenderPreviewArea ()
{
for (int x = (int)ZoneStart.x; x < (int)ZoneEnd.x; x++)
{
for (int z = (int)ZoneStart.y; x < (int)ZoneEnd.y; z++)
{
if (Physics.Raycast(new Vector3(x, 0, z), new Vector3(1, 0, 1), 0.65f))
{
PreviewHolder = (GameObject)Instantiate(selector.selectedPart, new Vector3(x, 0, z), Quaternion.identity);
PreviewHolder.name = string.Format("Preview Area at ({0}, 0, {1})", x, z);
BuiltPreview.Add(PreviewHolder);
}
}
}
}
All help is appreciated and I'm always open to new solutions if you can't fix the script above.
Comment