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 Langynom · Aug 12, 2014 at 03:29 PM · aipathfindingwaypointsnodes

Problems with my waypoints code

I have been working on a waypoint function that gives the route between two points in the form of an array and it was working fine until I added an extra waypoint. Now it ignores the waypoint when finding routes even if it is clearly part of a shorter route and if it is the goal unity crashes all together. I have looked through my code for the problem and I'm yet to find anything and I was wondering if anyone had an insight.

 static var nodes = new Array ();  //will contain nodes coordinates
 static var joins = new Array ();  //will contain nodes joined to each node
 
 var cube : GameObject;  //Temporary, used as graphical indicator of points
 
 nodes[0] = Vector3(-5,0,5);
 nodes[1] = Vector3(10,0,10);
 nodes[2] = Vector3(5,0,-10);
 nodes[3] = Vector3(5,0,0);
 nodes[4] = Vector3(-10,0,5);
 nodes[5] = Vector3(10,0,5);  //Set's up the nodes coordinates
 nodes[6] = Vector3(0,0,5);
 
 joins[0] = new Array(2,3,4);
 joins[1] = new Array(3,5);
 joins[2] = new Array(0,5,3);
 joins[3] = new Array(1,2,0);
 joins[4] = new Array();
 joins[4][0] = 0;
 joins[5] = new Array(1,2);  //sets up nodes neighbours.
 joins[6] = new Array(1,0);
 
 function Start () 
 {
     for(i=0; i<nodes.length; i++)
     {
         Instantiate(cube, nodes[i], Quaternion.identity);
     }
 }  //Temporarry, places graphical indicators at node points
 
 
 function Update ()
 {
     for(i=0; i<joins.length; i++)
     {
         for(x=0; x<joins[i].length; x++)
             Debug.DrawLine(nodes[i], nodes[joins[i][x]], Color.red);
     }
     //Draws debug Lines between all neighbour nodes
 }
 
 static function route(start : int, end : int)
 {
     if(start==end)
         return(Array(start,end));
     var x=0;
     var openNodes = new Array (); //Will contain any nodes that haven't reached a dead end
     var reached = new Array ();  //Will contain [0]the previous node in it's chain and [1]if it has been reached in the search
     openNodes[0] = new Array (start, Vector3.Distance(nodes[start], nodes[end]), 0); //Sets initial openNodes value to being search at start
     var reachedGoal=false; //Variable used to exit while loop
     for(i=0; i<nodes.length; i++)
     {
         reached[i] = new Array(null,false);
     }  //Initialises all values at null and false for the reached array
     reached[start][1]=true;// sets the start node to be 'reached'
     while(!reachedGoal)
     {
         var smallest : float;
         var activeNode : int;
         var activeTravelled : float;
         var newNode : int;
         var newTravelled : float;
         var activeOpen : int;
         var firstCheck=true;
         var newNodePrevious : int; //Various variables used inside the while loop
         
         
         
         for(i=0; i<openNodes.length; i++) //For each entry in openNodes...
         {
             activeNode=openNodes[i][0]; //Set the selected entry as active Node...
             activeTravelled=openNodes[i][2];  //And the distance traveled to reach it from the start node as active travelled...
             for(n=0; n<joins[activeNode].length; n++)  //Then search each neighbour node...
             {
                 var distanceCheck=activeTravelled+Vector3.Distance(nodes[activeNode],nodes[joins[activeNode][n]]);  //and calculate the distance it will take to reach that node using the distance tavelled for the previous node +the distance between the two nodes
                 if((firstCheck||distanceCheck+Vector3.Distance(nodes[joins[activeNode][n]],nodes[end])<smallest)&&!reached[joins[activeNode][n]][1]) //To check if this is either the first comparison or if it is the shortest path yet, taking into account the straight line distance to the end node...
                 {
                     newNode=joins[activeNode][n];
                     smallest=distanceCheck;
                     newTravelled=distanceCheck;
                     firstCheck=false;
                     newNodePrevious=activeNode;  //If it is, set it to be the new node, as well as some variables used in the next step
                 }
             }
         }
         reached[newNode][1]=true;//Set the final new node to be 'reached'
         reached[newNode][0]=newNodePrevious;//And set which node was before it in the chain
         openNodes[openNodes.length]=new Array(newNode, Vector3.Distance(nodes[newNode],nodes[end]), newTravelled);//Finally create a new entry for the newNode in the open Nodes array
         if(newNode==end)
             reachedGoal=true; // If the new node is the goal break the loop, if not repeat until the goal is reached
     }
     var lastNode=end; //Set a variable to hold the last checked loop in the chain(which should be the goal)
     var routePath=new Array(); //Create an array to hold the path
     routePath[0]=lastNode;//Set the first result to be the end
     while(lastNode!=start)
     {
         lastNode=reached[lastNode][0];
         routePath[routePath.length]=lastNode;
     }//Work bacwords using the previous node information to fill the routePath with the (reversed) start to end path
     return(routePath);//Return the path information
 }

Above is my script for the function itself and below is how I call it

         var firstCheck=true;
         var endFirstCheck=true;
         var shortest=0.0;
         var endShortest=0.0;
         for(i=0; i<waypoints.nodes.length; i++)
         {
             var distanceCheck=Vector3.Distance(transform.position, waypoints.nodes[i]);
             if(firstCheck||distanceCheck<shortest)
             {
                 startNode=i;
                 shortest=distanceCheck;
                 firstCheck=false;
             }
             distanceCheck=Vector3.Distance(GameObject.Find("player").transform.position, waypoints.nodes[i]);
             if(endFirstCheck||distanceCheck<endShortest)
             {
                 endNode=i;
                 endShortest=distanceCheck;
                 endFirstCheck=false;
             }
         }//Checks for the closest node and sets it as the start point
         path=waypoints.route(startNode,endNode);// runs the routing script and sets up the pth array
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

1 Reply

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

Answer by tanoshimi · Aug 12, 2014 at 03:35 PM

Your while(!reachedGoal){} loop will never break if the endNode can't be reached (for example, if node 6 is your goal) - once the openNodes list is empty, if you still haven't been able to reach the end you should return null or something similar.

Comment
Add comment · Show 2 · 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 Langynom · Aug 12, 2014 at 03:40 PM 0
Share

Thanks, I will add that in, it may just be me being blind but I can't see why it wouldn't be able to reach node 6? Never the less this is something I need to implement so thank you!

avatar image Langynom · Aug 13, 2014 at 10:31 AM 0
Share

It should not have taken this long to realise that the nodes only have their own "joined" set up and hasn't been put in the other nodes "joined" section, thanks for pointing me in the right direction, and definatley for the other suggestion, problem solved

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

21 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

Related Questions

Why isn't a Ai Waypoint Spawning??? 0 Answers

What is the best AI system for pathfinding on a moving platform 2 Answers

Storing Vector3's and Detecting Gameobjects 1 Answer

Waypoint tree structure 2 Answers

Waypoints and AI pathfinding 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