• 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 /
  • Help Room /
avatar image
4
Question by Waffle_Dragon · Jun 04, 2016 at 05:10 PM · c#navmeshpathfindingnavigationnav mesh

Navmesh How to check if full path available? C#

I want to have a build mode where you can place buildings on a grid. Then you can press a Ready button to spawn enemy units that will use navmesh to find their way to a target position. But I don't want players to be able to leave the build mode if there is no path available to the target position. So I want to calculate a path from spawn position to target position and if it is available, store the path, and flag a bool.

Outside of build mode, I want all units to use the same path that was already calculated. What I have so far almost does this. Using the CalculatePath() function however, returns true if just part of the path is available. I want it to only return true if a complete path is available. Otherwise you can leave build mode and the units end up only going part of the way.

alt text

As you can see, the enemy unit (Green capsule) finds the shortest path to the target position (Blue cube) and stops part way because there is no path from there to the target position. I dont want this. I want the path bool to only be true if the complete path is available, not just part of the path.

Here is the manager script I'm using to calculate the path.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyUnitManager : MonoBehaviour {
 
     public NavMeshAgent spawnPosition;
     Transform targetPosition;
 
     [HideInInspector]
     public bool pathAvailable;
     public NavMeshPath navMeshPath;
 
     void Start() {
         navMeshPath = new NavMeshPath();
         targetPosition = GameObject.FindGameObjectWithTag("Target").transform;
     }
 
     void Update() {
         pathAvailable = spawnPosition.CalculatePath(targetPosition.position, navMeshPath);
     }
 }
 

And this is the script for the enemy unit.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(NavMeshAgent))]
 public class Enemy : MonoBehaviour {
 
     public EnemyUnitManager unitManager;
 
     NavMeshAgent unit;
 
     void Start () {
         unit = transform.GetComponent<NavMeshAgent>();
     }
 
     void Update () {
         if (Input.GetButtonDown("Jump")) {
             if (unitManager.pathAvailable == false) {
                 Debug.Log("Path not available");
             }
             else {
                 unit.SetPath(unitManager.GetComponent<EnemyUnitManager>().navMeshPath);
             }
         }
     }
 }
 

untitled.png (60.5 kB)
Comment
Add comment · Show 2
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 Mmmpies · Jun 04, 2016 at 08:17 PM 0
Share

I've been away from Unity for a while (got sucked into the Elite Dangerous time vacuum!) but I don't think navmesh can act as dynamically as that.

However if you're doing something like an RTS and unit/building placement is on a grid as you've shown you could write your own to check the quickest path till it hits an object and then try alternate routes. $$anonymous$$ight a bit of a handful to program but a grid of locations shouldn't be too expensive computationally.

avatar image Waffle_Dragon Mmmpies · Jun 05, 2016 at 01:05 AM 0
Share

Thanks for the reply. I've found a way for navmesh to check if the path is complete, I'll post it below.

I was originally going to do what you suggested with A*, but then I discovered unity's navmesh system. It is dynamic enough to recalculate the path and change it in real time, but I want to calculate the path once and apply it to all units so they aren't calculating their own path. Less computationally heavy. I'm not making something as dynamic as an RTS, although I think navmesh would be fine for an RTS, it's more like a turn based strategy game. Since you won't be placing buildings when enemy units are spawning.

I got navmesh to do what I wanted with it, and it seems to run well, but since I'm developing for mobile I want the system to be as less expensive computationally as possible.

5 Replies

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

Answer by Waffle_Dragon · Jun 05, 2016 at 01:17 AM

Solved It. I'll post it here for anyone else with the same problem.

Instead of using the bool returned on the CalculatePath() function, you can check the path status after you have calculated the path.

if (path.status != NavMeshPathStatus.PathComplete) {}.

For anyone interested this is what I did for my code.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyUnitManager : MonoBehaviour {
 
     public NavMeshAgent spawnPosition;
     Transform targetPosition;
 
     [HideInInspector]
     public bool pathAvailable;
     public NavMeshPath navMeshPath;
 
     void Start() {
         navMeshPath = new NavMeshPath();
         targetPosition = GameObject.FindGameObjectWithTag("Target").transform;
     }
 
     void Update() {
         if (/*UI Button Press*/) {
             if (CalculateNewPath() == true) {
                 pathAvailable = true;
                 print("Path available");
             }
             else {
                 pathAvailable = false;
                 print("Path not available");
             }
         }
     }
 
     bool CalculateNewPath() {
         spawnPosition.CalculatePath(targetPosition.position, navMeshPath);
         print("New path calculated");
         if (navMeshPath.status != NavMeshPathStatus.PathComplete) {
             return false;
         }
         else {
             return true;
         }
     }
 }
 

As to keep the computational cost down I am calculating the path only when a button is pressed and then checking if the path reaches the target position. I only continue if this is true. I then assign the already calculated path to each enemy unit when they spawn. So they are only updating movement and not calculating their own path.

This works fine for my purposes, but if you would like to update the path while units are spawning, you can call the CalculateNewPath() function more often or put it in Update() and that path will be assigned to all your units. Or you can have each unit calculate their own path with SetDestination()

Comment
Add comment · Show 4 · 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 LegolasTai · Feb 17, 2017 at 03:57 AM 0
Share

This is useful, thanks

avatar image TheDeveloper10 · Apr 17, 2020 at 09:20 AM 0
Share

4 years later... Thanks man!

avatar image jyblackshaw · Nov 25, 2020 at 12:42 AM 0
Share

Thank you so much man

avatar image Gua · Apr 01 at 06:31 PM 0
Share

Unfortunately this does not work on large complex levels. Here's demonstration.

https://youtu.be/uqT5c6JM5oM

avatar image
1

Answer by FiveFingerStudios · Dec 02, 2018 at 06:49 AM

2 years later....thanks!

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 SuggestiveStranger · Jun 25, 2021 at 01:40 AM 0
Share

5 years later ...thanks!

avatar image
0

Answer by CSDiabo · Feb 10, 2019 at 11:10 AM

2019 and this answer still helped me, thanks a lot!

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

Answer by pedrociva · Oct 30, 2020 at 02:19 AM

Here we are in 2020 hahaha, Thanks buddy !

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

Answer by Scrabblem · Jan 04 at 10:20 AM

2022 and this is still helpful :D Thanks man :D

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

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

13 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

Related Questions

Navigation 0 Answers

How do I generate a good navmesh for uneven procedurally generated terrain? 2 Answers

make a self-driving car, combine Navimesh and waypoint? 1 Answer

Farthest Reachable Destination 0 Answers

Multi-Floor, Levels pathfinding with Navmesh 2 Answers


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