Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question View Circle in 2D Topdown

Discussion in '2D' started by _RC, Sep 9, 2022.

  1. _RC

    _RC

    Joined:
    Aug 21, 2011
    Posts:
    45
    Hello!

    In a 2D Topdown project, i want to have a view circle around the player to show areas hidden by walls.
    It works like this:
    • the lower tile of the walls have a box collider
    • raycasts from the player position collide with those colliders
    • the hit point positions are used to generate a mesh used as a mask
    • this mask hides upper wall and ceiling tiles within it
    The problem is, that ceiling tiles can be on the same tile as lower wall tiles.
    Then the raycasts can never reach this area, and so these tiles are never within the mesh mask.


    Can be seen in the second half this video:


    Eventually it could work if the raycasts would be allowed to go "into" the box colliders, but not get out again if that makes any sense.

    I am really out of ideas here :-/
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,520
    Different idea: get cells underneath player. Alpha the tiles with tilemap.SetColor(cell, alphadColor). This would let you do a 50% alpha which might look pretty good. You can see a similar effect in for example Hades, see here at 20 second mark. When you're behind a pillar, it alphas. This would also be slightly cheaper too.

    You could even collect cells more widely below the player and feather in/out the alphaing with more transparency toward the center. In all cases, you'd need to keep track of the alpha'd cells to ensure you restore them to opaque.

    You don't necessarily need to use the physics system to find these tiles. You could collect from a tilemap dedicated to tall objects.
     
  3. sebela

    sebela

    Joined:
    Oct 13, 2013
    Posts:
    9
    I see your problem there,
    Are your ceiling tiles & floor tiles actually the same tile or two separate tiles on top of each other? I'm wondering if you couldn't make them part of two different layers and then cast & mask only the layer that you want.
    Otherwise Lo-renzo's idea sounds like a nice and potentially cheaper alternative
     
  4. _RC

    _RC

    Joined:
    Aug 21, 2011
    Posts:
    45
    If i do it per pixel or per tile does not matter (which is the cheaper alternative if you got you right?).
    Those tiles are already on different tilemaps within the same grid, and the mask already only affects the upper wall and ceiling tiles.

    I want only areas to be affected which are visible to the player, which means areas that can be hit by a raycast from the player. But those raycasts can never hit those ceiling tiles when a ground wall tile (which of course block visibility) is behind it
     
  5. _RC

    _RC

    Joined:
    Aug 21, 2011
    Posts:
    45
    In case anyone cares, this is my solution to it:
    upload_2022-9-12_0-17-2.png
    I extruded the generated mesh by the height of one tile upwards. It is now a view-egg instead of a view circle, but it does exactly what i wanted it to do :)
     

    Attached Files:

    sebela likes this.