- Home /
How to stop placing a building on an obstacle
I am trying to make a city builder game and my problem is that I can't figure out a good way how to make a script that would not let a building to be placed if there is another building placed in that position or just an obstacle. I tried making a script with triggers to stop buildings from being built, but that didn't seem to work as I expected, it gave me a quite big fps drop whenever I was moving my mouse over the buildings with a buildable building selected and sometimes I could even build overlapping the other buildings . Here are my scripts. Any help would be appreciated.
Building placement script
#pragma strict
private var hit : RaycastHit;
var gridSize : float = 1.0;
private var canBuild = false;
var BuildableBuilding : GameObject;
function Update () {
var ray : Ray;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit, 100.0f))
{
if (hit.collider.name=="Ground")
{
GameObject.FindWithTag("Building").transform.position = hit.point + Vector3(0,0.5,0);
GameObject.FindWithTag("Building").transform.position.x = Mathf.Round(GameObject.FindWithTag("Building").transform.position.x / gridSize) * gridSize;
GameObject.FindWithTag("Building").transform.position.z = Mathf.Round(GameObject.FindWithTag("Building").transform.position.z / gridSize) * gridSize;
}
}
}
function OnGUI() {
if (GUI.Button(Rect(10,70,50,30),"Build"))
{
Debug.Log("Choose a place to build");
canBuild = true;
Instantiate(BuildableBuilding, hit.point, Quaternion.identity);
canBuild = false;
}
}
FinishBuilding script
#pragma strict
var newMat : Material;
var oldMat : Material;
var finishedBuilding : GameObject;
private var canBuild = true;
function Update () {
if (Input.GetButton ("Shoot") && canBuild == true)
{
Instantiate(finishedBuilding, transform.position, transform.rotation);
Destroy(gameObject);
}
}
function OnTriggerStay (other : Collider) {
if(other.gameObject.tag=="BuildingDone")
{
canBuild = false;
renderer.material = newMat;
print("new material");
}
}
function OnTriggerExit (other : Collider) {
if(other.gameObject.tag=="BuildingDone")
{
canBuild = true;
renderer.material = oldMat;
print("old material");
}
}
If these buildings are going to be placed like a grid, you could store what cells have buildings in using a 2D array.
Before placing a new building or when hovering over a space if the array has a value for that cell highlight it red or just dont allow it, then if there is nothing there add the building and fill that cell in the array.
Answer by TomatoOrgyLT · Mar 01, 2014 at 09:19 AM
Alright, I think I solved my problem. The reason why I could sometimes build on the other buildings is because it didn't know how many triggers it had to exit. For example if I placed 2 buildings near each other and tried to place a third one on them I could do that because the trigger would be exiting one of my buildings and not both so all I had to do is count how many triggers does it enter and leave. Here is the final code.
#pragma strict
var colliderCount : int = 0;
var newMat : Material;
var oldMat : Material;
var finishedBuilding : GameObject;
function Update () {
if (Input.GetButton ("Shoot") && colliderCount == 0)
{
Instantiate(finishedBuilding, transform.position, transform.rotation);
Destroy(gameObject);
}
}
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag=="BuildingDone")
{
colliderCount++;
renderer.material = newMat;
print("new material");
}
}
function OnTriggerExit (other : Collider) {
if(other.gameObject.tag=="BuildingDone")
{
colliderCount--;
if(colliderCount == 0)
{
renderer.material = oldMat;
print("old material");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613133846im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to check whether gameObject is on even ground? 1 Answer
Creating a 68x68 grid of trigger cubes 2 Answers
Up to date Unity building placement code/tutorials? 1 Answer
RTS building snap to grid 2 Answers
Problems making game grid for space rts. (just visible to player not highlighting grid cells) 1 Answer