/// <summary> /// Update targets list /// </summary> void FindVisibleTargets() { VisibleTargets.Clear(); // Get all targets in radius Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, ViewRadius, TargetMask); for (int i = 0; i < targetsInViewRadius.Length; ++i) { //Transform target = targetsInViewRadius[i].transform; Lightable lightable = targetsInViewRadius[i].GetComponent <Lightable>(); if (lightable == null || targetsInViewRadius[i].transform.name == transform.name) { continue; } Vector3 rayOrigin = transform.position + RayOffset; Vector3 dirToTarget = (lightable.RayTargetPoint - rayOrigin).normalized; // For each target, check if it's in angle float angle = Vector2.Angle(new Vector2(transform.forward.x, transform.forward.z), new Vector2(dirToTarget.x, dirToTarget.z)); if (angle < ViewAngle / 2) { // +1 to avoid weird ray inconsistency across frames float distToTarget = Vector3.Distance(rayOrigin, lightable.RayTargetPoint) + 1; if (lightable.name.ToLower().Contains("angel") && drawGizmo) { Debug.DrawRay(rayOrigin, dirToTarget * distToTarget, Color.red, 1f / UpdateRate); } // Check if there isn't any obstacle between source and target RaycastHit[] hits = Physics.RaycastAll(rayOrigin, dirToTarget, distToTarget, ObstacleMask); RaycastHit nearestHit = new RaycastHit { distance = float.MaxValue }; foreach (RaycastHit hit in hits) { if (hit.transform == transform) { continue; } if (hit.distance < nearestHit.distance) { nearestHit = hit; } } if (nearestHit.transform == lightable.transform) { VisibleTargets.Add(lightable); } } } }
void OnTriggerExit2D(Collider2D c) { Lightable l = c.gameObject.GetComponent <Lightable>(); if (l) { lightables.Remove(l); if (lightOn) { l.Unlight(this); } } }
void OnTriggerEnter2D(Collider2D c) { Lightable l = c.gameObject.GetComponent <Lightable>(); if (l) { lightables.Add(l); if (lightOn) { l.Light(this); } } }
// Use this for initialization void Start() { if (Player == null) { Player = gameObject; } _playerStatus = GetComponent <Lightable>(); _battery = GetComponent <FlashlightBattery>(); _ool = GetComponent <OnOffLight>(); //le joueur démarre la lampe allumé _playerStatus.LightSources = 1; _ool.OnToggle += (on) => { if (on == true) { _playerStatus.LightSources += 1; } else { _playerStatus.LightSources -= 1; } }; _fov = GetComponent <FieldOfView>(); _baseAngle = _fov.ViewAngle; if (_baseAngle + AngleIncrease > 360) { AngleIncrease = 360 - _baseAngle; } _baseLightAngle = _ool.Lights[0].spotAngle; // To determine joypad or keyboard inputManager = GameObject.Find("GameManager").GetComponent <InputManager>(); if (Player.name == "Player2") { RInput = Rewired.ReInput.players.GetPlayer(inputManager.Player2Index); } else { RInput = Rewired.ReInput.players.GetPlayer(inputManager.Player1Index); } _ool.ToggleOff(); }
void Light() { isLightOn = true; lightEffect.SetActive(true); int layerMask = 1 << LayerMask.NameToLayer("Interactable"); Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, lightAOE, layerMask); Instantiate(lightAnimation, transform.position, Quaternion.identity, transform); foreach (Collider2D col in colliders) { Lightable l = col.GetComponent <Lightable>(); if (l) { l.Light(); } } }