Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TomatoOrgyLT · Feb 17, 2014 at 10:18 PM · gridrtsplacement

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");
         }
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thaiscorpion · Feb 18, 2014 at 12:31 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
             }
         }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skaulakis · Feb 28, 2020 at 10:27 AM 0
Share

Very good .

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check whether gameObject is on even ground? 1 Answer

RTS building placement collision problem C# 1 Answer

RTS Style building help 2 Answers

unity - place objects by format 1 Answer

Grid system visualization 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges