Search Unity

Least expensive way of checking if player is in area

Discussion in 'Scripting' started by santiagolopezpereyra, Dec 4, 2018.

  1. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    Well, I'm scripting an AudioManager (one of many things my prototype is still missing) and I want to have an environment-based music system. This means that different musics are played depending on where the player is; if he's in a cave, dark music should sound; if he's in a meadow, more cheerful tunes should be played.

    To check where the player is, my first idea is to create trigger colliders on, for example, the desert areas, and, OnTriggerEnter, set an Environment enum to the value Desert. This is the enum, on the EnvironmentManager class:

    Code (CSharp):
    1. public enum Environment {Meadow, Desert, Cave}
    2. public static Environment CurrentEnvironment;
    So, on trigger enter, something as the following should run:

    Code (CSharp):
    1. void OnTriggerEnter(Collider col){
    2.                 // Check if the player entered the area
    3.         if (col.GetComponent<FPCamera>()){
    4.                         EnvironmentManager.CurrentEnvironment = Environment.Desert;
    5.     }
    The AudioManager would keep track of the current environment the player is in, and play a musica accordingly.

    The only simple question I have is the following: having a trigger collider the size of a desert area (big) checking what colliders enter and exit its radius should be expensive, I figure. Am I wrong? If I'm right, and it is in deed expensive, what other alternatives I have for checking if player is in a determined environment?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    Size of a collider doesn't really impact the performance. The testing of overlapping boxes is the same arithmetic steps regardless of size.

    What I would suggest is not calculating that trigger against everything. And instead having a special layer for it. Like say a "RegionalEvent" layer or something. And only those objects that you're concerned about intersecting with it can do so.

    You would do this with the Layer Collision Matrix:
    https://docs.unity3d.com/Manual/LayerBasedCollision.html
     
  3. Also I wouldn't check for the Camera component. Use (name or tag) and layer instead. In theory you have player tag or something similar at the first place. Searching for a component every time when something is entering in a collider is not the most performant way to do it.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If your map isn't PCG you can just map a color-coded textured that says what color is where and what color means what environment, map the player position on the map to the texture map and viola.

    I also bet there are plenty of way to make a map like this in a procedural world depending on your generation algorithm.