//assorbe luce da una sorgente private void ControlLight() { if (Input.GetButtonDown("AbsorbLight")) //se premo il tasto destro { Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, absorbRange)) //raycast dal centro dello schermo fino a distanza absorbRange { //print("colpito qualcosa"); LightSource2 ls = hit.collider.gameObject.GetComponent <LightSource2>(); LightTrigger lt = hit.collider.gameObject.GetComponent <LightTrigger>(); if (ls) //se è una sorgente di luce { int light_amount = ls.takeLight(); if (light_amount > 0) //se è accesa --> prendo luce { Light += light_amount; if (lt) { lt.Trigger(); } } else if (Light >= ls.getLightBulletsAmount()) // se è spenta e ho munizioni --> rilascio luce { ls.PutLight(); Light -= ls.getLightBulletsAmount(); if (lt) { lt.Trigger(); } } } } if (Physics.Raycast(ray, out hit, doorOpenRange)) //secondo raycast più corto per vedere se posso aprire una porta { UnlockedDoor ud = hit.collider.gameObject.GetComponent <UnlockedDoor>(); if (ud) { ud.Open(); } } } }
private IEnumerator CountLight() { while (true) { Collider[] hitColliders = Physics.OverlapSphere(transform.position, radius, lightMask); //check all lights in a sphere of radius r int lightNumber = 0; foreach (Collider c in hitColliders) { RaycastHit hit; //check if the light is not behind a wall if (Physics.Raycast(transform.position, (c.transform.position - transform.position), out hit, Mathf.Infinity, wallMask)) { LightSource2 l = hit.collider.GetComponent <LightSource2>(); if (l != null && l.isLit()) //if the light is ON, increases light count { lightNumber++; } } } agent.speed = baseSpeed - 0.75f * lightNumber; //decrease agent speed based on lights number yield return(new WaitForSeconds(3)); } }