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
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Show a piece of the minimap

Discussion in 'Scripting' started by Redden44, Aug 1, 2016.

  1. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    152
    Hey, I have a dungeon and a minimap both made of prefab rooms and when the player enter a room of the dungeon I need to show a piece of the minimap.

    I thought about two solution and I'd like to know which one is better/ligther.

    First solution, I could check the position of the player every frame and transform the dungeon coordinates in minimap coordinates; then I can use these coordinates to find the right room of the minimap, by checking the children of the minimap, where each child is a sprite of a room. Finally I can activate the sprite of the right room.

    Second solution, I could add a boxCollider2D to each room, as big as the room itself, and when the player enters it, I could use the OnTriggerEnter2D to get the coords of the room and then procede like solution one.

    Which one is the lighter solution? Do you have any other ideas? Thanks.
     
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    557
    Hey ! You should avoid using physics when you don't need it, looking up player position and calculating its grid coordinates is order of magnitude cheaper than having extra colliders.
     
    Redden44 likes this.
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,768
    One solution I've used in the past is to use a second camera, positioned above the map looking down. You can set the camera size or distance from the map or FOV to match the size of the room, and position it at the appropriate coordinate to view the room, or set it to follow the player. You can choose which layers to render in this camera if you don't want to show certain assets, and this way the minimap will always be up to date to the room you're in, without needing new assets for every new room or changed room. The camera can render as a small area on screen, with optionally less detail for better performance.
     
    Redden44 likes this.
  4. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    152
    @met: I'll go with the code then, I'm always scared to put stuff in the Update function hehe

    @Jeffrey: showing just one room it's an interesting solution, but I need the minimap to show up to the whole dungeon, if the player esplores all of it.

    Thanks both for the help :)
     
  5. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    557
    Hey np !

    For the reccord using update is totally fine, what is bad is having Update on thousands of objects as you'll get the overhead from unity but up to a few hundreds objects it may be just fine.

    If you don't use vsync (or fps limiting) and your game doesn't use lots of CPU you might want to consider FixedUpdate to avoid heating up computers but if you're barely above 60 fps avoid FixedUpdate like its the plague.

    Following those rules you can use Update/LateUpdate/FixedUpdate at will :)
     
  6. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    152
    What about a timer to check the position only 10 times a second instead of every frame? Could it be lighter or the difference would be minimal making it pointless? Thanks.

    This is the function I call to check the room btw:
    Code (CSharp):
    1. public void CheckMapRoom(Vector2 playerPos)
    2. {
    3.     int x = (int)(playerPos.x / roomSize);
    4.     int y = (int)(playerPos.y / roomSize);
    5.     _playerIcon.transform.position = new Vector2(x, y);
    6.     if (_mapVisibility[x, y] == false)
    7.     {
    8.         foreach (Transform child in _mapParent.transform)
    9.         {
    10.             if (child.position.x == x && child.position.y == y)
    11.             {
    12.                 child.GetComponentInChildren<SpriteRenderer>().enabled = true;
    13.                 _mapVisibility[x, y] = true;
    14.                 return;
    15.             }
    16.         }
    17.     }
    18. }
     
    Last edited: Aug 3, 2016
  7. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    557
    You should fire up the profiler and check it out, its a great learning resource about how to do things in unity.

    The only part of your code that might be costly is the foreach if mapparent constains lots of children. Also you shouldn't compare float value directly, someday you'll get a wrong result without knowing where it comes from, try Mathf.Approximately() or Mathf.RoundToInt() to make sure your code is reliable regardless of floating precision.
     
  8. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    152
    I can't use Mathf.RoundToInt(), because I need to truncate the float to get the room coords; I didn't know about Mathf.Approximately() though, thanks ^^

    I'll leave the code as it is for now, the parent may have around 12-25 children, it should be fine.

    Thanks again for the help :)
     
unityunity