Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ThaGuvNa · Sep 01, 2020 at 03:27 AM · loopingbooleans

Enable NavMeshObstacle when all units are out of the way

Hello, I am working through creating an RTS and I've hit a wall with yet another problem while building a building.

I need to be able to place a building foundation on the ground for my units to begin working on. I can detect when I place the building over one of my units. I check to see if the Object has the Unit class, and check for a boolean "isPlayerObject". This works fine, and I can even tell the unit to move to the edge of the building's bounds.

The problem is with my NavMeshObstacle. I don't want the act of placing the building to push my Unit agent out of the bounds, I want them to walk to the edge. So I need some mechanism to detect when all units have moved out of the way.


Here's what I've got so far, but I feel like I'm severely overcomplicating this, AND it really only means the first unit to slow to a velocity of .1 triggers the NavMeshObstacle to be enabled... meaning other units may get pushed out of the bounds at the last second. What I really need to know is how to wait to enable the NavMeshObstacle until all units in the unitsToMove array are TRUE.

      unitsToMove = Physics.OverlapBox(buildLoc, buildExtent); // Create an array of colliders that are currently in the building
     for(int i = 0; i < unitsToMove.Length; i++)
     {
         if (unitsToMove[i].GetComponent<Unit>() && unitsToMove[i].GetComponent<ObjectInfo>().isPlayerObject) //Are they a unit, and do they belong to the player?
         {
             Unit unitToMove = unitsToMove[i].GetComponent<Unit>();

             destination = new Vector3(Random.Range(buildMax.x, buildMin.x), 0, buildMax.z); //Get random range between buildMin & buildMax for X, Y, Z

             unitToMove.Move(unitToMove.agent, destination); //Call the Unit's Move function and move to the random spot along the edge of the building.
             StartCoroutine(MovingOutOfBuilding(buildingCollider)); //Constantly check to see if this unit has slowed down.
         }
     }

  IEnumerator MovingOutOfBuilding(NavMeshAgent agent, Collider buildingCollider)
 {
     while (true)
     {
         yield return new WaitForSeconds(1);
         if (agent.velocity.magnitude < .1f)
         {
             buildingCollider.GetComponent<NavMeshObstacle>().enabled = true;
             yield break;
         }
     }
 }

If you can't tell I'm pretty new at this, and really hopeful someone with some experience can point me in the right direction. Thanks in advance!!

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

134 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 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 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 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 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 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 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

Loop through a List to check whether a bool is true within a ScriptableObject 1 Answer

Stop animation after playing once 1 Answer

iTween: uneven looping with PutOnPath 1 Answer

Why is my shoot animation always looping? 1 Answer

Problem with seamless music looping in Unity 5 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