Search Unity

OverlapSphere only works on a semi-sphere

Discussion in 'Scripting' started by Shoker-Gun, Jun 27, 2021.

  1. Shoker-Gun

    Shoker-Gun

    Joined:
    Jun 27, 2021
    Posts:
    1
    Hi,
    I'm trying to add a Field of View for my AI like in this tutorial:
    . The problem I'm getting is that it seems that the OverlapSphere only works on the half of the sphere it is supposed to. Also the Gizmo I use with the same parameters shows a Sphere properly, so I have no clue what the error is.

    (In the image below, Up equals to the Z axis and right to the X axis)
    Semi-sphere.png
    The code is the following:
    private void FieldOfViewCheck()
    {
    Collider[] rangeChecks = Physics.OverlapSphere(transform.position, inner_radius+external_radios_offSet);
    List<Agent> enemigos = new List<Agent>();
    foreach (Collider ene in rangeChecks)
    {
    Agent enemy = ene.GetComponent<Agent>();
    if(enemy!=null){
    if (enemy==this.GetComponent<Agent>()) //Checking the agent is not itself
    break;
    if ( equipo!=null && enemy.equipo!=null){ //Checking they are from different teams

    if(equipo.id==enemy.equipo.id){
    break;
    }

    }

    Transform target = enemy.transform;
    Vector3 directionToTarget = (target.position - transform.position).normalized;

    //Debug.Log(Vector3.Angle( angleVectorFromFront(0), directionToTarget));
    //This would show properly the angle between the agent and the enemy. And it stops if you
    // leave the red are of the picture. So I'm pretty sure the problem is with the SphereCollider


    if (Vector3.Angle( angleVectorFromFront(0), directionToTarget) < (close_angle+broad_angle_offSet) / 2)
    { //angleVectorFromFront(0) works as transform.forward
    float distancia = Vector3.Distance(transform.position, target.position);

    if (!Physics.Raycast(transform.position, directionToTarget, distancia, mascaraObstaculos)){
    //Checking there is not an obstacle in between
    enemigos.Add(enemy);
    enemigo = enemy;
    }
    }
    }
    }
    if(enemigos.Any())
    actualizaEnemigo(enemigos);
    }


    I hope you can help me, thank you for your time :)