Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Vector2.distance not thread safe or is it a bug?

Discussion in 'Scripting' started by dudester, Feb 9, 2016.

  1. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    Hi all so i was making my voxel engine ,its a multi-threaded engine , i get the players position through Update , store it then check if a chunk is a certain distance away from the player , then i deleete it,Now I noticed something odd , if i travel from the centre of the world outwards for some reason the engine starts unloading chunks right below me , so either the vector2 class is no longer thread safe and unity forgot to add a warning or theres a bug in unity somewhere , its not in the position system , cause when i debug it everything is correct , i'm wondering if maybe there is an error in the distance class maybe or something , my distance checking is quite simple its just if(Vector2.Distance(PlayerPos, chunkPos )>DistanceToload){
    unloadchunk}
    Any ideas would be extremely helpful as i've tried my hardest to fix the problem (been at it for a day now).

    Code (CSharp):
    1. void checkActiveChunks(){
    2.                 if(ActiveChunks.Count>0){
    3.                     for(int i = 0;i < ActiveChunks.Count;i++){
    4.                         VoxelChunk chunk = ActiveChunks[i];
    5.                     Vector2 chunkpos = new Vector2(chunk.m_pos.x ,chunk.m_pos.z)/TriSize[chunk.lodLevel];
    6.  
    7.                     Vector2 playerpos = new Vector2(ChunkPosition.x,ChunkPosition.z);
    8.                     if(Vector2.Distance(chunkpos,playerpos)>=distanceToLoad+25){
    9.                             if(chunk.HasChanged && chunk.shouldrender)
    10.                                 chunk.SaveVoxels();
    11.                             Trash.Add(chunk);
    12.                         m_voxelChunk.Remove((int)(chunk.RealPos.x),(int)(chunk.RealPos.z));
    13.                             if(ActiveChunks.Contains(chunk)){
    14.                                 GrassChunks.Remove(chunk);
    15.                                 ActiveChunks.Remove(chunk);
    16.                             }
    17.                             return;
    18.                         }
    19.  
    20.                     }
    21.                 }
    22.  
    23.  
    24.  
    25.         }
    Full code if it helps
     
    Last edited: Feb 9, 2016
  2. dudester

    dudester

    Joined:
    Oct 25, 2014
    Posts:
    371
    Solved , turns out i needed to devide by the minlod in order to get the correct position