1

Basically on my map I'm trying to use Physics.CheckBox detect when the player is in certain areas to determine where my enemies will spawn at. I am using a layer mask to detect when its colliding with the player and Gizmos to visualize this box in the editor. The issue I'm having is that it will return true even when the player isn't inside the box. I have verified every single other game item does not have the player layer mask causing it to return true when it hits something else. The kicker is Physics.CheckSphere works perfectly except for the fact that my map is square, not circle, so I can't use check sphere because I can't cover all of the areas I need to cover.

Code for both is as follows, note that both of these lines are not in my script at the same time I alternated them out for testing:

atNeighborhood = Physics.CheckSphere(spawnAreas[0].transform.position, neighborhoodRange, playerLayer); 
atNeighborhood = Physics.CheckBox(spawnAreas[0].transform.position, neighborhoodRange, Quaternion.identity, playerLayer);

Why would the CheckBox return true when colliding with items not in the layer mask but the CheckSpere works perfectly and only returns true when colliding with the player? Anyone have any idea?

The702Guy
  • 31
  • 2
  • Hold on for a minute, I have an answer coming – gbe Aug 30 '22 at 01:22
  • There is a small trap with the Box declaration. When you set the size you actually set the "half extend" meaning that the box is twice as big as you might have expected. Is this the case for you here? – Voidsay Aug 30 '22 at 13:05
  • Ahh that may be it, is there a way to visualize that with the gizmos? Maybe just do neighborhoodRange * 2? – The702Guy Aug 30 '22 at 13:47

1 Answers1

0

LET ME KNOW IF THERE ARE ANY PROBLEMS OR ERRORS IN COMMENTS. THANKS!

Ok. CheckBox can get kind of confusing sometimes. I would reccomend something else.

You could use Empty Game Objects with colliders on them and put them where ever you want. IsTrigger must be set to true. Imagine these as "zones", where whenever you step in one, something can happen.

All you have to do is set a certain tag to each zone to activate different things.

Note: Player does not need rigidbody, but it would be a whole lot less messy if you did.

Here is a script if your player does have a rigidbody (put this script on your player):

void OnTriggerEnter(Collider obj)
{
   if (obj.gameObject.CompareTag("Zone 1"))
   {
       SpawnZombies();
   }
}

Player doesn't have rigidbody:

If your player does not have a rigidbody, you could put a bunch a script on each one called "zone activator". Important Notes for this version:

  1. Your player must have a collider and a unique tag.
  2. On each zone add a rigidbody.
  3. Make sure detectCollisions is false!
  4. Make sure useGravity is false!
  5. This zone detector should have it's collider be a trigger; (You do not want this thing to move!)

You can now create a script that goes on each zone:

public string message;
public bool inZone;

void OnTriggerEnter(Collider obj)
{
   if (obj.gameObject.CompareTag("player"))
   //Or set it to whatever tag the player has
   {
       inZone = true;
   }
}
void OnTriggerExit(Collider obj)
{
   if (obj.gameObject.CompareTag("player"))
   //Or set it to whatever tag the player has
   {
       inZone = false;
   }
}

You must then reference this in the player's script

public ZoneDetector[] allZones;
void Update()
{
   //.....
   foreach (ZoneDetector zone in allZones)
   {
       if (zone.inZone == true)
       {
           if (zone.message == "zone 1")
           {
               DoZone1();
           }
           if (zone.message == "zone 2")
           {
               DoZone2();
           }
       }
   }
}
gbe
  • 1,003
  • 1
  • 7
  • 23
  • Thank you for the reply, the zone one is interesting and I may try to give that a go if no one can answer my question. I use triggers in other cases but based on how I wanted to implement this I thought Physics.CheckBox would work great. I don't know why its popping true when colliding with incorrect layer masks. Like I said, CheckSphere works perfectly but CheckBox pops true when it shouldn't. – The702Guy Aug 30 '22 at 02:53
  • @The702Guy I will try to come up with a new solution a new answer. – gbe Aug 30 '22 at 22:24
  • @The702Guy can you open the physics debug window, and tell me what you see? thanks. – gbe Aug 30 '22 at 22:33