Patrol.Patrulha C# (CSharp) Method

Patrulha() public method

public Patrulha ( ) : void
return void
    void Patrulha()
    {
        pulseTime -= Time.deltaTime;

        if(pulseTime < 0){
         scannedColliders = Physics.OverlapSphere(myTransform.position,16,enemyMask);//
           if(scannedColliders.Length > 0)
         enemyAround = true;
           else enemyAround = false;
          pulseTime = 2.0f;
        }

        if(enemyAround){

          // Fase de teste
          if(scannedColliders.Length > 1){
        float tempdistance = Mathf.Infinity;
        for(int i = 0; i < scannedColliders.Length; i ++ ){
          if(!scannedColliders[i]){
            Debug.LogWarning("Scanned colliders are empty somehow");
            return;
          }
          target = scannedColliders[i].transform;
          targetEntity = target.GetComponent<CBaseEntity>();
          Debug.Log("targetEntity: " + targetEntity);
          if(!targetEntity) return;
          if(targetEntity.Type == CBaseEntity.eObjType.Building) continue;
          float tempdistanceindex = Vector3.Distance(myTransform.position,target.position);
          if(tempdistanceindex < tempdistance){
            tempdistance = tempdistanceindex;
            break;
          }else{
            target = scannedColliders[0].transform;
            targetEntity = target.GetComponent<CBaseEntity>();
          }
        }

          }else{
        target = scannedColliders[0].transform;
        targetEntity = target.GetComponent<CBaseEntity>();
          }

          targetVector = target.position - myTransform.position;

           if(Vector3.Distance(myTransform.position,target.position) < detectionDistance
          && Vector3.Angle(targetVector,myTransform.forward) < detectionRadius){
        if(targetEntity.Type == CBaseEntity.eObjType.Building) return;
          pulseTime = 2.0f;
          status = eAlertLevel.DETECT;
          Debug.LogWarning("Angle enemy is found: " + Vector3.Angle(targetVector,myTransform.forward));
          existingAlert = GameObject.Instantiate(detectAlert,new Vector3(target.position.x,target.position.y + 15, target.position.z), Quaternion.identity) as GameObject;
           }else if(Vector3.Distance(myTransform.position,target.position) < detectionDistance
                && Vector3.Angle(targetVector,myTransform.forward) < alertRadius){
          status = eAlertLevel.ALERT;
           }

        }else{
          target = null;
        }

        Debug.DrawRay(myTransform.position,myTransform.forward * 10);
    }