- Home /
Infinite loop with "for" that I'm really bad at finding.
Hello, firstly I'm not the best programer world has to offer, I'm fully aware of that, but I do want to make a videogame, and so far so good actually, but I ran into a bit strange infinite loop for my top-down-2d AI script that I fail to understand.
Everyhing before this part works fine. But after it an infinite loop goes somewhere in the following codepiece and Unity just freezes without responding to anything.
public bool AINoWall(int wpcount,string rupert){
Vector3 AIWaypointMark = GameObject.Find (rupert).transform.position;
float AIDeltaX = AIWaypointMark.x - AiMark.x;
float AIDeltaY = AIWaypointMark.y - AiMark.y;
Vector2 AIWaypointDirection = new Vector2 (AIDeltaX, AIDeltaY);
RaycastHit2D WallHitWhileTrackingWaypoint = Physics2D.Raycast (AiMark, AIWaypointDirection, AIWaypointDirection.magnitude, Wall);
if (WallHitWhileTrackingWaypoint)
return (false);
else
return (true);
}
public bool PlayerNoWall(int wpcount,string Alex){
Vector3 PlayerWaypointMark = GameObject.Find (Alex).transform.position;
float PlayerDeltaX = PlayerWaypointMark.x - PlayerMark.x;
float PlayerDeltaY = PlayerWaypointMark.y - PlayerMark.y;
Vector2 PlayerWaypointDirection = new Vector2 (PlayerDeltaX, PlayerDeltaY);
RaycastHit2D WallHitWhileTrackingWaypoint = Physics2D.Raycast (PlayerMark, PlayerWaypointDirection, PlayerWaypointDirection.magnitude, Wall);
if (WallHitWhileTrackingWaypoint)
return (false);
else
return (true);
}
void Update () {
PlayerMark = GameObject.Find ("The_Player").transform.position;
AiMark = GameObject.Find ("Zombie").transform.position;
Xdif = PlayerMark.x - AiMark.x;
Ydif = PlayerMark.y - AiMark.y;
float distance=650000;
int ZombieVertex = 0;
int PlayerVertex = 0;
wpcount = 0;
for ( wpcount=1;wpcount-1 < MaxWaypoints;wpcount++ ){
float AIdeltaX = AiMark.x - GameObject.Find(AllWaypointsNames[wpcount]).transform.position.x;
float AIdeltaY = AiMark.y - GameObject.Find(AllWaypointsNames[wpcount]).transform.position.y;
float AIZ = Mathf.Sqrt(Mathf.Pow(AIdeltaX,2) - Mathf.Pow(AIdeltaY,2));
if (AIZ<distance && AINoWall(wpcount,AllWaypointsNames [wpcount])){
distance = AIZ;
ZombieVertex=wpcount;
}
}
distance=650000;
wpcount = 0;
for ( wpcount=1;wpcount-1 < MaxWaypoints;wpcount++ ){
float PlayerdeltaX = PlayerMark.x - GameObject.Find(AllWaypointsNames[wpcount]).transform.position.x;
float PlayerdeltaY = PlayerMark.y - GameObject.Find(AllWaypointsNames[wpcount]).transform.position.y;
float PlayerZ = Mathf.Sqrt(Mathf.Pow(PlayerdeltaX,2) - Mathf.Pow(PlayerdeltaY,2));
if (PlayerZ<distance && PlayerNoWall(wpcount,AllWaypointsNames [wpcount])){
distance = PlayerZ;
PlayerVertex=wpcount;
}
}
distance=650000;
wpcount = 0;
int DestinationVertex = robert.getnextvertex(ZombieVertex,PlayerVertex);
robert.renew();
The infinite loop is somewhere around here, but I just can't find it (Well technically I can, but I completely fail to understand)! And God knows I tried. Would appreciate a lot, if anyone could help. Because I was stuck here for like more than 14 hours in a row trying to solve this non-stop and now my brain feels like a poridge and my eyes look like a tomato.
I probably doesn't cause the freeze, but the amount of GameObject.Find() you are running in the Update() can probably make it pretty slow (especially if $$anonymous$$axWaypoints is a large number).
As for the loops, did you try printing values of the variables you are using in the definition (wpcoint, $$anonymous$$axWaypoints) as you run the loop and see how they change?
This is a strange way to code a loop, not sure I understand your full intent.
Where do you assign the value to $$anonymous$$axWayPoints?
Well, it worked surprisingly well even on weak machines( despite GameObject.Find )
I did print them, but at some point everything ok, then it just stops printing them, because script is no longer working. Besides they look absolutely normal.
$$anonymous$$axWaypoints is the ammount Waypoints(vertexes) for the use in dijkstra, I asign them earlier for dijkstra pathfinding algorythm.
$$anonymous$$y full intent was to make the enemy pursue a player in a room in simple manner just by following him and basicly avoid obsticles. Then, if player leaves room or enemy's view on player's position is obstructed by wall, he would switch from pursue mode to search mode ( travelling between the waypoints based on where the monster thought player would go ), then ( that is asu$$anonymous$$g player got away ) he would hang around aimlessly, just looking for the player, but not searching for him ( that part i haven't done yet ).
So for that purpose i made 2 "for's" cycles to find the closest waypoint, that is not behind the wall, and then pursue it, once enemy reaches waypoint, he would ask dijkstra what waypoint to pursue next, and so on, untill he sees the player and starts chasing him ( like that last part from the Ter$$anonymous$$ator movie ).
Your answer
Follow this Question
Related Questions
Loop problem 1 Answer
Getting an error with my for/in loop! 3 Answers
How to have a (wait) yield on a for loop? 0 Answers
How to put spacing between Gameobjs in loop 3 Answers
How to handle long loops 3 Answers