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. Dismiss Notice

Suptract 2 areas and spawn objects in resulting area

Discussion in '2D' started by iboshido, Apr 20, 2021.

  1. iboshido

    iboshido

    Joined:
    Mar 4, 2017
    Posts:
    33
    I have a 2D top down game. The camera follows the player. I would like to spawn enemies in random positions around the player. But for performance reasons i dont want to fill the whole map with enemies. When enemies are out of view, they will be destroyed and at the same time new enemies will spawn in the new area.

    The problem is, that i dont know how to spawn enemies in an area that is the result of the subtraction between the new area and the old area, cuz i dont want to destroy the enemies from the old area which are also in the new area.

     
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    well... how do you decide what gets destroyed right now?

    what exactly is your "area" definition?
    why cant you just say that anything outside of your display is able to despawn?

    Edit:

    ooooh nvm I understand now, you are despawning enemies while they are still visible on camera, and spawning them while they are not visible right now, because the camera is following a bit behind the player?
     
    Last edited: Apr 20, 2021
  3. iboshido

    iboshido

    Joined:
    Mar 4, 2017
    Posts:
    33
    exactly
     
  4. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    well if you define an area by center of the area, width and height you can tell wether a point is inside using:

    Code (CSharp):
    1.     public bool isInsideArea(Vector2 positon, Vector2 center, float width, float height)
    2.     {
    3.         Vector2 max = center + new Vector2(width, height) * 0.5f;
    4.         Vector2 min = center - new Vector2(width, height) * 0.5f;
    5.         if (positon.x <= max.x && positon.y <= max.y)
    6.         {
    7.             if (positon.x >= min.x && positon.y >= min.y)
    8.             {
    9.                 return true;
    10.             }
    11.         }
    12.         return false;
    13.     }
    thus if you want to only despawn if objects are outside of the area of the player, as in this picture:

    all you need to do is
    Code (CSharp):
    1.         if(!isInsideArea(enemyPositon, playerPosition, spawnAreaWidth, spawnAreaHeight))
    2.         {
    3.             //Despawn enemy
    4.         }
    this would despawn anything that is not in the green area


    if you want to additionally avoid to despawn visible enemies that are outside the spawnzone, all you need to do is:
    Code (CSharp):
    1.         if(!isInsideArea(enemyPositon, playerPosition, spawnAreaWidth, spawnAreaHeight)
    2.             && !isInsideArea(enemyPositon, cameraPosition, screenWidthToWorldCord, screenHeightToWorldCord))
    3.         {
    4.             //Despawn enemy
    5.         }
    which means that anything outside this green area gets despawned:


    in the same way you could also "substract" areas by simply removing one of the ! signs

    just for completenes sake, this is what you initially asked for:
    Code (CSharp):
    1.         if(isInsideArea(enemyPositon, playerPosition, spawnAreaWidth, spawnAreaHeight)
    2.             && !isInsideArea(enemyPositon, cameraPosition, screenWidthToWorldCord, screenHeightToWorldCord))
    3.         {
    4.             //Spawn enemy
    5.         }
    which would only spawn enemies inside this green area:
     
    Last edited: Apr 20, 2021
    iboshido likes this.
  5. iboshido

    iboshido

    Joined:
    Mar 4, 2017
    Posts:
    33
    I think thats exactly what i needed. I will try this out. Thank you so much ! :)
     
  6. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    you can also do it like minecraft by dividing your map in chunks

    when you move your characters place them in a dictionary that corresponds to each chunk

    then whenever your player gets far enough from a given chunk you despawn everything in that chunk