Search Unity

simple pathfinding, closest path fails

Discussion in 'Scripting' started by Donfrankos, Jul 24, 2011.

  1. Donfrankos

    Donfrankos

    Joined:
    Jul 24, 2011
    Posts:
    8
    Hi
    Im trying to create a simple pathfindingsystem, where an object has to move ONLY by roads (set by the player) to its destination. All the roads that are placed by the player are put in an arraylist. The way I thought to do this was to get the object its coordinates, check the array if there's a road left, right, up, down from that, if so put those in a seperate array (so a new array containing the possible directions to move), and then iterate through that array to find which way is closest to the goal coordinates. Then use that closest value as new position to check from, and do it all over again to finally reach the destination... every time it should add the closest position to the patharray so in the end the object can use that to move.
    The problem I have is it only returns the last index value of the new created array, not the closest..
    the code I created:

    Code (csharp):
    1. function CalculatePath(){
    2.  
    3. CurrentLocation = Vector2(transform.position.x,transform.position.z);
    4.  
    5. var WayPointCheck = new Array();
    6. var DistanceArray = new Array();
    7.  
    8. //check right side
    9. if(RoadBlocksArray.PositionList.IndexOf(CurrentLocation + Vector2(1,0)) >= 0){
    10.    
    11.     WayPointCheck.Push(CurrentLocation + Vector2(1,0));
    12. }
    13.  
    14. //check left side
    15. if(RoadBlocksArray.PositionList.IndexOf(CurrentLocation - Vector2(1,0)) >= 0){
    16.    
    17.     WayPointCheck.Push(CurrentLocation - Vector2(1,0));
    18. }
    19.  
    20. //check Up side
    21. if(RoadBlocksArray.PositionList.IndexOf(CurrentLocation + Vector2(0,1)) >= 0){
    22.    
    23.     WayPointCheck.Push(CurrentLocation + Vector2(0,1));
    24. }
    25.  
    26. //check Down side
    27. if(RoadBlocksArray.PositionList.IndexOf(CurrentLocation - Vector2(0,1)) >= 0){
    28.    
    29.     WayPointCheck.Push(CurrentLocation - Vector2(0,1));
    30. }
    31.  
    32. var distance = Mathf.Infinity;
    33. var closest: Vector2;
    34. var Goal = Vector2(50,30);
    35.  
    36. for(var ClosestPoint: Vector2 in WayPointCheck){
    37.  
    38.     var diff = (ClosestPoint - Goal);
    39.     var curDistance = diff.sqrMagnitude;
    40.  
    41.    
    42.        if (curDistance < distance) {
    43.             closest = ClosestPoint;
    44.         PathArray.Add(closest);
    45.         }
    46.    
    47.    
    48.  
    49. }
    50.  
    51.  
    52. print(closest);
    53.  
    54. }
    I dont get it why I only get the last index value every time. Next to this Im not sure if this is really a good idea (performance wise).

    in a nutshell:

    1) I have a building (goal)
    2) I have an object
    3) object is only allowed to move over roads (drawn by player, and put in seperate array)
    4) I want the object to get from its position to the building taking the shortest route (in case there are multiple roads leading to the building)
    5) Im using Vector2 for all position coordinates

    Im probably overseeing something, or am just plain stupid..

    I hope somebody can help me with this
    thanks in advance!!

    regards
    Frank
     
    Last edited: Jul 25, 2011
  2. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
  3. Donfrankos

    Donfrankos

    Joined:
    Jul 24, 2011
    Posts:
    8
    Thanks for the tip Zine92! But I rather try to create something myself though (makes it more fun and a good challenge!).

    I still don't get it, when I use the same loop and use the positions of certain tagged gameobjects (the loop which is in the Unity scripting ref.), then it does work..tested on a cube and the cube will translate to the closest goal
    It only doesn't seem to work with raw 2dvectors from an array.

    I neither get the part where

    Code (csharp):
    1. if(currentDistance <  Mathf.Infinity){
    2.  
    3. }
    currentDistance will always be smaller than infinity, so I guess thats why it only shows the last value from the array, in stead of the closest one...but then again why DOES it work when I use the transform from certain tagged gameobjects position??in the end the data used by the loop are also just vector2s

    I'm still lost! (and staring too long at my code probably)

    cheers
    Frank
     
  4. Donfrankos

    Donfrankos

    Joined:
    Jul 24, 2011
    Posts:
    8
    turned out I had a major brainfart..

    for those running in the same thing:


    Code (csharp):
    1.  
    2. var Goal = Vector2(50,30);
    3. var Closest = (WayPointCheck[0] - Goal).sqrMagnitude;
    4. var ClosestPoint = WayPointCheck[0];
    5.  
    6. for(i=0; i < WayPointCheck.length;i++){
    7.  
    8.      WayPointInArray = (WayPointCheck[i] - Goal).sqrMagnitude;
    9.  
    10.  
    11.             if( (WayPointInArray < Closest) ){
    12.  
    13.             ClosestPoint = WayPointCheck[i];
    14.  
    15.             }
    16.  
    17. }
    18.  
    19. PathArray.Push(ClosestPoint);
    20.  
    thanks anyways for reading!

    regards
    Frank