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.

Question Filling closed area in Unity 3D

Discussion in 'Scripting' started by hazarss, Oct 19, 2022.

  1. hazarss


    Aug 16, 2021
    Hey everyone!

    I'm working on an old-school gaming idea, where the player needs to occupy some amount of the map to complete the level (video link provided at the end).

    The empty area in the video is filled with objects (meshrenderer is disabled) when the player touches the boxes in that area, meshrenderer gets activated. And when the player gets back to a safe spot (borders) those objects become the new border.

    My question is, how can I fill the inside of that closed shapes? As you can see in the video, the player can travel and occupy new areas, but I want those areas to be filled completely.

    Could you please help me with that?

    P.S: I'm kinda new to Unity game development, and I would really appreciate it if you can take this into consideration while sharing your answers :) (I read some articles about flood fill, but don't know if that's the solution I need and how I can implement it into my case)
  2. Kurt-Dekker


    Mar 16, 2013
    The algorithm you seek is called "floodfill."

    I actually just made a 2D version of this game, which was originally Qix in the arcade, and later Xonix on the PC.

    You can find all kinds of psuedocode implementations, or see mine here:

    It floodfills on each side of the line, then tries to see which side the enemies are on and fills the other side if possible.