bounds versus boxColliders

Hello,

I have a large terrain for which I want to define 150 regions. As the controller moves over the terrain it will enter and exit regions and “stuff will happen”, so far just displaying unique text for each region.

I’ve successfully tried two methods.

  1. Cover the terrain with 150 bounds stored in an array and loop through the array every frame to see which bound contains the controller’s position.

  2. Cover the terrain with 150 empty gameObjects. Each gameObject has a trigger-enabled boxCollider attached. An OnTriggerEnter function attached to the controller does the rest.

Both methods are equally simple to set up, etc. So three questions, please:

  1. Which method will affect performance more?

  2. I tried various ways to associate multiple, individually detectable triggers with one mesh (the terrain) and the empty gameObject method described above was the only one thus far successful. Is there a better method?

  3. With the terrain seamlessly covered with boxColliders, will I also be able to detect collisions with future meshes positioned on the terrain?

I’m new at this.

–Rick

I think you can tweak the performance with the first method doing the check every few frames. Each region should be big enough so the player can’t change from A to B to C in less than a delta seconds. Knowing this, you can check the array only one time every delta seconds, and not every frame.

var delta = 0.5; // check 2 times every second
private var acumulative = 0.0;

function Update ()
{
   acumulative += Time.deltaTime;

   if ( acumulative >= delta )
   {
      acumulative = 0.0;
      
      // Check for player's position here
   }
}
var delta = 2; // check 1 time every 2 frames
private var acumulative = 0;

function Update ()
{
   acumulative ++;

   if ( acumulative >= delta )
   {
      acumulative = 0;
      
      // Check for player's position here
   }
}

So tweaking the delta variable, you should get a value higher enough to improve performance, but lower enough for the game logic.

There are special cases like corners or the player going back to the previous region after entering one… so, if he can do it all in one frame… then you will be forced to do it every frame.

Also consider that there is no need to check the player position on some special places, like near the middle of the every region.

If you are worried about speed then just store which bounds the player was inside last frame and start by checking that, in 99.9% of the time he’s going to still be inside that. Further more you can make each zone have an array of it’s 8 neighbors (which can easily be computed automatically on startup) and check those if the player isn’t inside the same bounds as last frame. Only if he’s not inside those (going reeaally fast, been teleported or what ever) you check all the zones.

Thanks for the optimization suggestions. I’m not worried about speed, really, but I’m wanting to learn good ways to do things from the start. So, again, thanks.