Search Unity

[SOLVED] - Endless background not working - if-condition not working properly !?!?

Discussion in 'Scripting' started by morvi, Feb 17, 2018.

  1. morvi

    morvi

    Joined:
    Feb 8, 2014
    Posts:
    10
    Hello community!

    I ran into a very strange issue while trying to build up aq routine to handle my background position itself according to the player.
    My background consists of 5x5 seamless pieces.
    There are 4 variables (currentLeft, currentRight, currentTop, currentBottom) that store the positions of the outside columns as well as of the row on top and bottom.

    A function named HandleBackgrounds() checks every frame if the player's coordinates are beyond the limits of these 4 variables. If so, the array is searched by a for loop to find the elements of the opposite side and moves them to the border, the player is about to reach so that the background is extend edin that direction. This is always done with a whole row or column (or both, if the player is near to one of the 4 corners of the background.

    Now the strange thing: This logic works for extending the background to the left or right, but it doesn't work fo top/down. When it comes to top/down there is always 1 piece of the top or bottom row, that is not moved. I debugged in visual studio, and I saw thast the colndition of the if-statement was true, but the if-block was not entered. I never saw something .like this.

    I'm no newbie to Unity, but for sure I am also no veteran. I am really stuck here, please help me. For sure, i made something wrong, but I simply cannot see it.

    This is the script, where it all happens:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BackgroundController : MonoBehaviour {
    6.     [SerializeField]
    7.     private float xSize, ySize;
    8.     [SerializeField]
    9.     private GameObject bg;
    10.  
    11.     [SerializeField]
    12.     private GameObject[] backgrounds;
    13.  
    14.  
    15.     Transform player;
    16.     Transform cam;
    17.     [SerializeField]
    18.     private float currentLeft, currentRight, currentTop, currentBottom;
    19.     // Use this for initialization
    20.     void Start () {
    21.         Initialize();
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.         if (!player)
    27.         {
    28.             FindPlayer();
    29.         }
    30.  
    31.         if (!player)
    32.         {
    33.             return;
    34.         }
    35.        
    36.         HandleBackgrounds();
    37.     }
    38.  
    39.     void Initialize()
    40.     {
    41.         cam = Camera.main.transform;
    42.         FindPlayer();
    43.     }
    44.  
    45.     void FindPlayer()
    46.     {
    47.         GameObject playerGO = GameObject.FindGameObjectWithTag("Player");
    48.         if  (playerGO)
    49.         {
    50.             player = playerGO.transform;
    51.         }
    52.     }
    53.  
    54.     /// <summary>
    55.     /// Called from Update().
    56.     /// Moving rows or columns (or both) of background, depending on player's position
    57.     /// relative to the top or bottom row or left or right column of background pieces.
    58.     /// These positions are always stored in currentLeft, currentRight, currentTop and
    59.     /// currentBottom.
    60.     /// xSize and ySize contain the sizes in world units of 1 piece.
    61.     /// player is transform of player's gameobject
    62.     /// </summary>
    63.     void HandleBackgrounds()
    64.     {
    65.         if (player.position.x > currentRight)
    66.         {
    67.             //move left column to right
    68.             for (int i = 0; i < backgrounds.Length; i++)
    69.             {
    70.                 if (backgrounds[i].transform.position.x == currentLeft) //this piece sits in the left column
    71.                 {
    72.                     Vector3 newPos = new Vector3(currentRight + xSize, backgrounds[i].transform.position.y, backgrounds[i].transform.position.z);
    73.                     backgrounds[i].transform.position = newPos;
    74.                 }              
    75.             }
    76.             //recalculating the positions of the new most left and most right columns
    77.             currentLeft += xSize;
    78.             currentRight += xSize;
    79.         } else if (player.position.x < currentLeft)
    80.         {
    81.             //move right column to left
    82.             for (int i = 0; i < backgrounds.Length; i++)
    83.             {
    84.                 if (backgrounds[i].transform.position.x == currentRight) //this piece sits in the right column
    85.                 {
    86.                     Vector3 newPos = new Vector3(currentLeft - xSize, backgrounds[i].transform.position.y, backgrounds[i].transform.position.z);
    87.                     backgrounds[i].transform.position = newPos;
    88.                 }
    89.             }
    90.             //recalculating the positions of the new most left and most right columns
    91.             currentLeft -= xSize;
    92.             currentRight -= xSize;
    93.         }
    94.  
    95.         if (player.position.y > currentTop)
    96.         {
    97.             //move bottom row up
    98.             for (int i = 0; i < backgrounds.Length; i++)
    99.             {
    100.                 if (backgrounds[i].transform.position.y == currentBottom) //this piece sits in the bottom row
    101.                 {
    102.                     Vector3 newPos = new Vector3(backgrounds[i].transform.position.x, currentTop + ySize, backgrounds[i].transform.position.z);
    103.                     backgrounds[i].transform.position = newPos;
    104.                 }
    105.             }
    106.             //recalculating the positions of the new bottom and top rows
    107.             currentBottom += ySize;
    108.             currentTop += ySize;
    109.         } else if (player.position.y < currentBottom)
    110.         {
    111.             //move top row down
    112.             for (int i = 0; i < backgrounds.Length; i++)
    113.             {
    114.                 if (backgrounds[i].transform.position.y == currentTop) //this piece sits in the top row
    115.                 {
    116.                     Vector3 newPos = new Vector3(backgrounds[i].transform.position.x, currentBottom - ySize, backgrounds[i].transform.position.z);
    117.                     backgrounds[i].transform.position = newPos;
    118.                 }
    119.             }
    120.             //recalculating the positions of the new bottom and top rows
    121.             currentBottom -= ySize;
    122.             currentTop -= ySize;
    123.         }      
    124.     }
    125. }
    I am using Unity 2017.2.0f3 Personal and Visual Studio Community 2017 Version 15.5.6 Background container gameobject containing the script.JPG Background piece that behaves wrong.JPG OK background piece.JPG Debugging in Visual Studio 1.JPG Debugging in Visual Studio 2.JPG

    I really appreciate any help!
    Thanks in advance
    Morvi

    Attachements:
    "Background container gameobject containing the script.jpg" shows the gameobject with the script and the filled variables in inspector. You can see that BG (17) (the "broken" piece, is element 17 in array backgrounds.
    "Background piece that behaves wrong.jpg" shows the piece of the top row that is not moved down, the others are behaving fine. You can see that the Y-coordinate in inspector is iodentical with currentTop in the inspector in the first image.
    "OK background piece.jpg": Just to give a fine example for comparing. You can also see that the y-coordinate is identical to the one that doesn't work
    "Debugging in Visual Studio 1.jpg" shows the moment when element 17 is checked. You can see at the bottom left in "Überwachen"-window (I'm german, sorry for that), that backgrounds.transform.position.y is equal to currentTop. Both are 21.6.
    "Debugging in Visual Studio 2.jpg" shows the next step - it didn't ernter the if-code-block, where it repositions element 17.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Then they aren't really equal. They just appear to be equal when converted to strings with some default format spec. At a bit level, they're different.

    This is not unusual with floats, and is why, in most cases, comparing floats with == is not a good idea. You could use Mathf.Approximately to do an approximate check, but better would be to come up with some other way of keeping track of what is the currentTop (and so on) other than looking at the positions of the transforms.
     
    morvi and tranos like this.
  3. morvi

    morvi

    Joined:
    Feb 8, 2014
    Posts:
    10
    OK, thanks for the hint. It really appears more often, yes? I will think about a Trigger-based solution then.

    Thank you very much!
    Morvi
     
  4. morvi

    morvi

    Joined:
    Feb 8, 2014
    Posts:
    10
    how can I mark your asnwer as the solution?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You don't mark solutions on the forums. You can just say "thanks" and optionally you can change your thread title to "Solved - (old thread name here)" if you want to. :)