Search Unity

How is Physics.OverlapSphere working regarding y coordinate

Discussion in 'Physics' started by dadom85, Oct 18, 2018.

  1. dadom85

    dadom85

    Joined:
    Oct 11, 2018
    Posts:
    1
    Hi,

    i am working on an EnemySight-Script which should detect if an enemy is within a defined viewRadius.
    It's partly based on this Tutorial.


    In his example it seems to work perfect, but the difference is that i am having some terrain where the ground is not always plane. How is OverlapSphere working with different Y coordinate level?

    Code (CSharp):
    1.        
    2. Collider[] lTargetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, targetMask);
    3.  
     
  2. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    You could probably use Physics.OverlapCapsule instead and have it go way up and below to compensate for that.
    If your targets have Rigidbodys you could also use OnTriggerEnter and OnTriggerExit.html (or OnTriggerStay) to create and manage a list of targets that are currently in a custom convex mesh collider (or Box/Sphere/Capsule Collider).