Search Unity

CheckSphere Accuracy

Discussion in 'Physics' started by SentientSkull, Jan 8, 2020.

  1. SentientSkull

    SentientSkull

    Joined:
    Apr 26, 2013
    Posts:
    75
    I'm using Physics.CheckSphere to check positions on a NavMesh for spawning objects.

    Basically something like:

    Code (CSharp):
    1. posAvailable = !Physics.CheckSphere(hit.position, spawnRadius, layerMask, QueryTriggerInteraction.Ignore);
    An issue I'm seeing is that if I spawn a lot of objects in a small space, and I have the radius set to the radius of the object, some of the objects overlap previously spawned objects. which means that CheckSphere returned false even though there was an overlap. If I use some padding, say the object radius is .5 and I use .8 for the CheckSphere radius, it seems to prevent overlapping.

    So is CheckSphere not always accurate, or am I doing something wrong?

    Also, will CheckSphere return true if it is completely encompassed in a collider (not actually overlapping)? If not, how can that situation be detected?
     
  2. SentientSkull

    SentientSkull

    Joined:
    Apr 26, 2013
    Posts:
    75
    I switched to CheckCapsule, and I'm having the same problem, some of the points will overlap with colliders, so CheckCapsule seems to be missing these collisions. Even if I set the radius to 3 some of the spawn points will overlap (and the prefab radius is only .25).

    Can anyone tell me why this is failing? If I can't trust the results than I don't know how to proceed.

    Edit: I think I've got it working. I had a layer mask that is set in the inspector and I had renamed the variable and forgot that it resets it to 'Nothing' (and probably some other issues in my code).
     
    Last edited: Jan 11, 2020
  3. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Good that you were able to figure out the problem. Just wanted to add on this point:

    I know the OnTriggerEnter() functions will definitely trigger on this scenario so I imagine CheckSphere will do the same. I haven't tested it but in my experience doing various physics checks it should work as expected--returning true.