Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

world streaming not working correctly

Discussion in 'Scripting' started by rycornx, Jul 23, 2020.

  1. rycornx

    rycornx

    Joined:
    Oct 4, 2017
    Posts:
    89
    I'm trying to have it so only the terrain tiles close to the player are active to cut on resource usage and so my computer doesn't crash. For some reason, my code doesn't work 100% as intended. I start with this, character at selescted
    upload_2020-7-23_1-8-57.png
    and end up with this, with these distance values
    upload_2020-7-23_1-10-21.png
    I don't understand why it deactivated the tile the player stands on even though it recorded the distance correctly and it is under the required amount?

    This is the code being used

    public class HideTiles : MonoBehaviour
    {
    [SerializeField]
    private string tileTag;

    [SerializeField]
    private Vector3 tileSize;

    [SerializeField]
    private int maxDistance;

    private GameObject[] tiles;

    public float[] dist = new float[16];

    // Use this for initialization
    void Start()
    {
    this.tiles = GameObject.FindGameObjectsWithTag(tileTag);
    DeactivateDistantTiles();
    }


    void DeactivateDistantTiles()
    {
    Vector3 playerPosition = this.gameObject.transform.position;
    int num = 0;

    foreach (GameObject tile in tiles)
    {
    Vector3 tilePosition = tile.gameObject.transform.position; //+ (tileSize / 2f);

    float xDistance = Mathf.Abs(tilePosition.x - playerPosition.x);
    float zDistance = Mathf.Abs(tilePosition.z - playerPosition.z);

    float distance = Mathf.Sqrt((xDistance * xDistance) + (zDistance * zDistance));

    dist[num] = distance; //getting the distance into the inspector
    num += 1; //
    if (distance > maxDistance)
    {
    tile.SetActive(false);
    }
    else
    {
    tile.SetActive(true);
    }
    }
    }

    void Update()
    {
    DeactivateDistantTiles();
    }
    }

    What's wrong here?

    Using unity 2019.3.11
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,012
    Please use code tags and check you don't have another instance of this scripting running on another object in your scene.

    If you type t:HideTIles in the Hierarchy search bar, that should show you if you do.
     
  3. rycornx

    rycornx

    Joined:
    Oct 4, 2017
    Posts:
    89
    • It is indeed the only instance in the scene.
    • I reworked whats called to get the distance, and made sure it gets the actual spot to check, middle of tile instead of bottom left corner.
    • It seems to work for everything but the bottom row and the second from the bottom on the right, they just refuse to appear no matter what.
    • I made some text to show the distance since reading from the inspector array became too complicated. Each text bit has reference to the checker and the tile under it, I then compied and pasted my code from the checker to have it get distance. For some god forsaken reason the check and text scripts return diferent calculated values despite being copied and pasted code
    • Sorry about the given code. It appeared to still be indeted before hitting post.
    • U can give more details later
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    8,455
    There's two things: when you paste code, please use the Paste-And-Match-Style feature instead, because otherwise sometimes the indenting gets munched. But the other thing is to add code tags, as outlined here: https://forum.unity.com/threads/using-code-tags-properly.143875/

    As for your actual problem, I recommend placing ONE tile in the scene, then you have only one piece of data to reason about, and drag your player around in the inspector window and make sure the "too far away" and "close enough to be visible" calculations for that one tile are correct. Don't try to do it with more than one tile until you have one tile working perfectly.
     
  5. rycornx

    rycornx

    Joined:
    Oct 4, 2017
    Posts:
    89
    I seem to have somewhat narrowed down the problem. the distance checker does work, calculates the distance and the hierarchy shows the object becomes active at the appropriate time. The issue is that while the hierarchy shows it's active it remains invisible. Also, I don't know if its GAIA acting up again but if bring in their player character the tiles that had been refusing to become visible suddenly do, however only if the character is within range even though it doesn't have a script to do so, the terrains becoming invisible or not depends on characters position and if my checker makes the tile active. if the character gets too far the terrain become invisible but still active
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    5,457
    It can be any number of things. Most likely you made a mistake when you calculate if you need to disable and/or enable a tile. Also it is possible that you properly hide the tile but then somewhere else you make it visible... We don't know you didn't share your relevant code.
     
  7. rycornx

    rycornx

    Joined:
    Oct 4, 2017
    Posts:
    89
    Code (CSharp):
    1. public class HideTiles : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private string tileTag;
    5.  
    6.     [SerializeField]
    7.     private int tileSize;
    8.  
    9.     [SerializeField]
    10.     private int maxDistance;
    11.  
    12.     public GameObject[] tiles;
    13.    
    14.     //show distacne calculated in inspector
    15.     public float[] dist = new float[16];
    16.  
    17.     // Use this for initialization
    18.     void Start()
    19.     {
    20.         this.tiles = GameObject.FindGameObjectsWithTag(tileTag);
    21.         DeactivateDistantTiles();
    22.     }
    23.  
    24.  
    25.     void DeactivateDistantTiles()
    26.     {
    27.         Vector3 playerPosition = this.gameObject.transform.position;
    28.         int num = 0;
    29.  
    30.         foreach (GameObject tile in tiles)
    31.         {
    32.             Vector3 tilePosition = tile.gameObject.transform.position;
    33.  
    34.             //get midle of tile instead of bottom left corner
    35.             tilePosition.x += 1024 / 2;
    36.             tilePosition.z += 1024 / 2;
    37.             tilePosition.y = playerPosition.y;
    38.  
    39.             float distance = Vector3.Distance(tilePosition, playerPosition);
    40.  
    41.            dist[num] = distance;
    42.             num += 1;
    43.             if (distance > maxDistance)
    44.             {
    45.                 tile.SetActive(false);
    46.             }
    47.             else
    48.             {
    49.                 tile.SetActive(true);
    50.             }
    51.         }
    52.     }
    53.  
    54.     void Update()
    55.     {
    56.         DeactivateDistantTiles();
    57.     }
    58. }
     
unityunity