Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Fewpwew130 · Jul 18, 2015 at 10:10 AM · navmeshpathfinding

check if an object is reachable

Hello, I am trying to create a simple movement script for bots. I created NavMesh, baked all static objects and added the agent.

Automatically I added some points which are the desirable locations for the bot to reach. Some of them lie outside the NavMesh. Is it possible to check: whether a target point is reachable or not? (lies in the NavMesh or outside)

Thank you very much!

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

2 Replies

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

Answer by Gatau · Jul 18, 2015 at 12:57 PM

The NavMesh.CalculatePath should be able to find which nodes are reachable inside the area mask.

http://docs.unity3d.com/ScriptReference/NavMesh.CalculatePath.html

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 Fewpwew130 · Jul 19, 2015 at 09:00 PM

Thank you for your help! The problem was in coordiates. I had different coordinates (local) for cubes and global for agent.

Here's the working script to go from one point to another. using UnityEngine; using System.Collections;

 public class randomcubepathgeneration : MonoBehaviour {
 
     //all target points
     public Transform target1;
     public Transform target2;
     public Transform target3;
     public Transform target4;
     public Transform target5;
     public Transform target6;
     public Transform target7;
     public Transform target8;
     public Transform target9;
     public Transform target10;
     public Transform target11;
     public Transform target12;
     public Transform target13;
     public Transform target14;
     public Transform target15;
     public Transform target16;
     public Transform target17;
     public Transform target18;
     public Transform target19;
     public Transform target20;
     public Transform target21;
     public Transform target22;
     public Transform target23;
     public Transform target24;
     public Transform target25;
     public Transform target26;
     public Transform target27;
     public Transform target28;
     public Transform target29;
     public Transform target30;
 
     //create array from points
     public Transform[] allpoints;
     public int randomnumber;
 
     public NavMeshAgent agent;
     public NavMeshPath path;
     public NavMeshPathStatus status; 
 
     public bool reachable = false;
     public bool hasFoundPath;
     public bool anewpath;
     public bool doonce = true;
 
     // Use this for initialization
     void Start () {
 
         allpoints = new Transform[30]; 
         allpoints [0] = target1;
         allpoints [1] = target2;
         allpoints [2] = target3;
         allpoints [3] = target4;
         allpoints [4] = target5;
         allpoints [5] = target6;
         allpoints [6] = target7;
         allpoints [7] = target8;
         allpoints [8] = target9;
         allpoints [9] = target10;
         allpoints [10] = target11;
         allpoints [11] = target12;
         allpoints [12] = target13;
         allpoints [13] = target14;
         allpoints [14] = target15;
         allpoints [15] = target16;
         allpoints [16] = target17;
         allpoints [17] = target18;
         allpoints [18] = target19;
         allpoints [19] = target20;
         allpoints [20] = target21;
         allpoints [21] = target22;
         allpoints [22] = target23;
         allpoints [23] = target24;
         allpoints [24] = target25;    
         allpoints [25] = target26;
         allpoints [26] = target27;
         allpoints [27] = target28;
         allpoints [28] = target29;
         allpoints [29] = target30;
 
         agent = GetComponent<NavMeshAgent> ();
         path = new NavMeshPath(); //without this declaration NavMesh.CalculatePath doesn't work (it doesn't even show FALSE)
         anewpath = true;
             if (anewpath = true) {
                 Debug.Log ("new target");
                 //choosing random point
                 randomnumber = Random.Range (0, allpoints.Length);
                 hasFoundPath = agent.CalculatePath (allpoints [randomnumber].position, path);
                 Debug.Log (hasFoundPath);
                 anewpath = false; //wait till the player gets to the point 
             }
         }
     
     // Update is called once per frame
     void Update () {
 
         if((path.status == NavMeshPathStatus.PathComplete) && (doonce == true));
                 {
                     print("The agent can reach the destionation");
                     reachable = true;
                     Debug.Log (status);
                     doonce = false;
 
                         if (reachable) {
                             //go to this point (the action continues even after MB was pressed)
                             agent.SetDestination (allpoints[randomnumber].transform.position);
                         }
                     
                 }
 
         if((transform.position.x == allpoints[randomnumber].transform.position.x)&&(transform.position.z == allpoints[randomnumber].transform.position.z)){
             Debug.Log ("reached destination");
             anewpath = true;
             doonce = true;
 
             if(anewpath){
                 randomnumber = Random.Range (0, allpoints.Length);
                 hasFoundPath = agent.CalculatePath (allpoints [randomnumber].position, path);
                 Debug.Log (hasFoundPath);
                 anewpath = false;
             }
 
         }
     }
 }
 
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

23 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

Related Questions

NavMesh.CalculatePath is acting weird 2 Answers

How to set NavMeshAgent Path with Vector3[] ? 2 Answers

Implementation of Navmesh terrain dijkstra algorithm 0 Answers

Trace a visible ray between two NavMeshAgent (Raycast) 0 Answers

A few questions about the Navigation Mesh stuff 0 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