Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Field of view in a tile based 2d game

Discussion in '2D' started by Yarbius, Sep 6, 2015.

  1. Yarbius

    Yarbius

    Joined:
    Apr 29, 2014
    Posts:
    22
    I have been building a turn based roguelike using prefabs for the terrain. I am trying to figure out an efficient way to determine the players field of view and lighting for a fog of war layer. I have been looking at options such as using Bresenham's line algorithm to ray cast around the player (Which would require about 100 rays for a view distance of 12) or some kind of shadowcasting.

    I am wondering if there is a way to use something built into Unity to do this. For each tile I need to determine if it is visible (Based on existing methods that return a bool) and the light level (Based on distance from the source)

    Edit: The existing methods just indicate if the tile blocks line of sight, not if it is visible itself.

    Any suggestions?
     
    Last edited: Sep 6, 2015
  2. ColossalPaul

    ColossalPaul

    Unity Technologies

    Joined:
    May 1, 2013
    Posts:
    174
    Are you trying to figure out if a tile is visible on screen and how far away it is from the camera?
     
  3. Yarbius

    Yarbius

    Joined:
    Apr 29, 2014
    Posts:
    22
    I am trying to determine if the tile is in the players 360 degree field of view. I have implemented this with a derivative of my Line of Sight algorithm and it seems to be performant enough. I just thought there might be some way to use Unity's built in raycasting or some other feature instead of brute forcing it like this.

    I am drawing a Bresenhams line from the player to each point on the perimeter of a square around the player and setting the alpha of a "Fog" prefab (that is in a layer between the Player and UI and the rest of the map and enemy sprites) to transparent. each time the fog layer is updated I am first setting any transparent fog sprites to 75% alpha before clearing the ones in the players current field of view to transparent so that previously visited areas are still visible but are tinted (Enemies and items automatically disable their sprite renderers when out of the players FoV so they do not show)

    Later I plan to cull any line over a certain length so that the field of view is more circular. I also plan to set different alphas, based on the distance from the player and the players light source to simulate lighting. I would also like to animate the alphas to simulate flights flickering

    In the future I may update the FoV algorithm to some form of shadowcasting but as it is now it is working for me.