0

I am writing a function for a game where I try to place "Path Tiles" from point A to point B.

I wrote a while loop to iterate tiles until I reach point B, I dont know why my loop is not stopping. I had to put a failsafe to make it stop.

I also think the code is very repetitive, so there my be a way to optimize it...

private void RoadBuilder(Vector2 pointA, Vector2 pointB, Image pathTile){
    float movingX = pointA.x;
    float movingY = pointA.y;
    float abDiffX = pointB.x - pointA.x;
    float abDiffY = pointB.y - pointA.y;
    bool xGo = true;
    // Debug.Log(pointA);
    // Debug.Log(pointB);
    // Debug.Log("Xdiff: "+abDiffX+" Ydiff: "+abDiffY);
    int failSafe = 0;
    while (movingX != pointB.x || movingY != pointB.y || failSafe != 50)
    {
        if (abDiffX > 0)
        {
            if (xGo){
                if (movingX != pointB.x) {movingX++;}
                xGo = false;
            }
            else{
                if (movingY != pointB.y){
                    if (abDiffY > 0) {movingY++;}
                    else {movingY--;}
                    xGo = true;
                }
            }
        }
        else
        {
            if (xGo){
                if (movingX != pointB.x) {movingX--;}
                xGo = false;
            }
            else{
                if (movingY != pointB.y){
                    if (abDiffY > 0) {movingY++;}
                    else {movingY--;}
                }
                xGo = true;
            }
        }
        foreach (MapTile tempTile in _mapList)
            if (
                tempTile.position.x == movingX && tempTile.position.y == movingY
                && tempTile.tileSprite != _tiles[1] && tempTile.tileSprite != _tiles[4]
                && tempTile.position != pointB
            ) {tempTile.tileSprite = pathTile;} 
        // Debug.Log("movingX: "+movingX+" movingY: "+movingY);
        failSafe++;
    }
}

Can anyone tell me what am I doing wrong? or if there is a better way to achive this?

Thanks

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Chris Luna
  • 43
  • 6
  • 1
    Two `float` values might be just a tiny bit off (especially with rounding errors), so the `==` operator will not evaluate to true. Usually you need to compare with a tolerance. See [comparing floating point numbers](https://stackoverflow.com/questions/1530069/comparing-floating-point-values) – John Wu Nov 09 '21 at 03:58
  • Thanks for the tip I'll go read it right now. :D – Chris Luna Nov 09 '21 at 04:32
  • Unity offers [`Mathf.Approximately`](https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html) which basically rather equals using `if(Mathf.Abs(x - y) <= Mathf.Epsilon)` where `Mathf.Epsilon` = smallest possible difference between two floats – derHugo Nov 09 '21 at 05:41
  • This question is about [tag:c#], not [tag:unityscript] – Ruzihm Nov 09 '21 at 20:52

1 Answers1

0

Try

while (movingX <= pointB.x || movingY <= pointB.y || failSafe <= 50)
Jon
  • 41
  • 5
  • I am trying to remove the whole failSafe, and only stop the while loop when movingX and movingY are equal to pointB.x and pointB.y. :D – Chris Luna Nov 09 '21 at 04:31
  • 1
    @kiner_shah - in fact this is an attempt to answer the question, and should not be deleted. – Wai Ha Lee Nov 09 '21 at 08:58