@@ -35,6 +35,7 @@ public void FindTarget()
3535 }
3636 }
3737 // Move towards target
38+ Debug . Log ( "Target node pos: " + targetNode . transform . localPosition ) ;
3839 MovementGen ( ) ;
3940 }
4041
@@ -69,7 +70,8 @@ void MovementGen()
6970 Debug . Log ( "All movement nodes added" ) ;
7071 // While movement nodes not empty
7172 KeyValuePair < GameObject , int > curNode = startNode ;
72- for ( int i = 0 ; i < movementNodes . Keys . Count ; i ++ )
73+ //for (int i = 0; i < movementNodes.Keys.Count; i++)
74+ while ( movementNodes . Count > 0 )
7375 {
7476 Debug . Log ( "Movement list size: " + movementNodes . Count ) ;
7577 // curNode = lowest cost node
@@ -90,20 +92,21 @@ void MovementGen()
9092 if ( ! finalCostNodes . ContainsKey ( curNode . Key ) )
9193 {
9294 finalCostNodes . Add ( curNode . Key , curNode . Value ) ;
93- }
94- //for each adj node of curNode
95- foreach ( KeyValuePair < GameObject , int > node in curNode . Key . GetComponent < TileCost > ( ) . adjacentTiles )
96- {
97- // if cost of curNode + move to adjNode cost < adjNode cost
98- if ( movementNodes . ContainsKey ( node . Key ) )
95+
96+ //for each adj node of curNode
97+ foreach ( KeyValuePair < GameObject , int > node in curNode . Key . GetComponent < TileCost > ( ) . adjacentTiles )
9998 {
100- if ( curNode . Value + node . Value < movementNodes [ node . Key ] )
99+ // if cost of curNode + move to adjNode cost < adjNode cost
100+ if ( movementNodes . ContainsKey ( node . Key ) )
101101 {
102- // adjNode cost = cost of curNode + move to adjNode cost
103- movementNodes [ node . Key ] = curNode . Value + node . Value ;
102+ if ( curNode . Value + node . Value < movementNodes [ node . Key ] )
103+ {
104+ // adjNode cost = cost of curNode + move to adjNode cost
105+ movementNodes [ node . Key ] = curNode . Value + node . Value ;
106+ }
104107 }
105- }
106108
109+ }
107110 }
108111 curNode = new KeyValuePair < GameObject , int > ( tileArray [ 0 , 0 ] , System . Int32 . MaxValue ) ;
109112 }
@@ -138,7 +141,7 @@ IEnumerator StartMovement(Dictionary<GameObject, int> finalCostNodes, KeyValuePa
138141 Debug . Log ( "New place" ) ;
139142
140143 Vector3 newPosition = nextNode . Key . gameObject . transform . localPosition ;
141- gameObject . transform . localPosition . Set ( newPosition . x , newPosition . y , transform . localPosition . z ) ;
144+ gameObject . transform . localPosition = new Vector3 ( newPosition . x , newPosition . y , transform . localPosition . z ) ;
142145 pos = nextNode . Key . GetComponent < TileCost > ( ) . GetPos ( ) ;
143146 Debug . Log ( "New pos: x" + newPosition . x + ", y " + newPosition . y ) ;
144147 curNode = nextNode ;
0 commit comments