private void GetNewTarget() { // TODO: This should be a "static" list modified on instantiation of new targetable objects. GameObject[] possibleTargets = GameObject.FindGameObjectsWithTag("CanTarget"); Vector3 reticulePos = Camera.main.ScreenToViewportPoint(_reticle.transform.position); Vector3 targetablePos; float shortestDist = Range; float currentObjectDistance = 0f; GameObject likelyTarget = null; for (int i = 0; i < possibleTargets.Length; i++) { targetablePos = Camera.main.WorldToViewportPoint(possibleTargets[i].transform.position); // Check if this object is in the screen. if (targetablePos.x > 0 && targetablePos.x < 1 && targetablePos.y > 0 && targetablePos.y < 1) { // Calculate screenspace distance from targeting reticule; currentObjectDistance = Vector3.Distance(targetablePos, reticulePos); // Check if this object is closer to the reticule than a previous object if (currentObjectDistance < Range) { likelyTarget = possibleTargets[i]; shortestDist = currentObjectDistance; } } } if (likelyTarget != null) { var destructable = likelyTarget.GetComponent <Destructable>(); Target = destructable; SetTargetUI(destructable); } }
internal void SetTargetUI(Destructable newTarget) { TargetUI.GetComponent <CanvasGroup>().alpha = 1f; TargetHealthBar.fillAmount = newTarget.Health / (float)newTarget.MaxHealth; TargetName.text = newTarget.Name; TargetDistance.text = string.Format( "{0}m", (int)Vector3.Distance(transform.position, newTarget.transform.position)); }
internal void SetTargetUI(Destructable newTarget) { TargetUI.GetComponent<CanvasGroup>().alpha = 1f; TargetHealthBar.fillAmount = newTarget.Health / (float)newTarget.MaxHealth; TargetName.text = newTarget.Name; TargetDistance.text = string.Format( "{0}m", (int)Vector3.Distance(transform.position, newTarget.transform.position)); }
void Awake() { Cursor.visible = false; _reticle = GameObject.Find("Reticle").GetComponent <Image>(); _turret = GameObject.Find("turret_tmp"); _playerShipControl = GetComponent <PlayerShipControl>(); _destructable = GetComponent <Destructable>(); ProjectileSpawnPoint = GameObject.Find("PProjectileSpawnPoint"); Projectile = (GameObject)Resources.Load("Projectiles/Projectile"); TargetName = TargetUI.transform.FindChild("TargetName").GetComponent <Text>(); TargetDistance = TargetUI.transform.FindChild("TargetDistance").GetComponent <Text>(); TargetingArrow = GameObject.Find("TargetIndicator").GetComponent <RectTransform>(); _destructable.OnHealthChanged += UpdateHealthBar; }
private void UpdateTargetUI() { // Can this reasonably and safely be registered to an event? float healthPercent = PlayerShipAction.Target.Health / (float)PlayerShipAction.Target.MaxHealth; if (healthPercent <= 0) { Target = null; TargetHealthBar.fillAmount = 1f; TargetName.text = ""; TargetDistance.text = ""; TargetUI.GetComponent <CanvasGroup>().alpha = 0.2f; return; } TargetHealthBar.fillAmount = healthPercent; TargetDistance.text = string.Format( "{0}m", (int)Vector3.Distance(transform.position, PlayerShipAction.Target.transform.position)); }
void OnTriggerEnter(Collider collider) { Destructable destructable = collider.gameObject.GetComponent <Destructable>(); if (destructable != null) { destructable.ModifyHealth(-Damage, Owner); if (!collider.CompareTag("Player")) { if (PlayerShipAction.Target == null) { PlayerShipAction.Target = destructable; Owner.GetComponent <PlayerShipAction>().SetTargetUI(destructable); } } } if (collider.CompareTag("Environment")) { gameObject.SetActive(false); } }
void Awake() { Cursor.visible = false; _reticle = GameObject.Find("Reticle").GetComponent<Image>(); _turret = GameObject.Find("turret_tmp"); _playerShipControl = GetComponent<PlayerShipControl>(); _destructable = GetComponent<Destructable>(); ProjectileSpawnPoint = GameObject.Find("PProjectileSpawnPoint"); Projectile = (GameObject)Resources.Load("Projectiles/Projectile"); TargetName = TargetUI.transform.FindChild("TargetName").GetComponent<Text>(); TargetDistance = TargetUI.transform.FindChild("TargetDistance").GetComponent<Text>(); TargetingArrow = GameObject.Find("TargetIndicator").GetComponent<RectTransform>(); _destructable.OnHealthChanged += UpdateHealthBar; }
private void UpdateTargetUI() { // Can this reasonably and safely be registered to an event? float healthPercent = PlayerShipAction.Target.Health / (float)PlayerShipAction.Target.MaxHealth; if (healthPercent <= 0) { Target = null; TargetHealthBar.fillAmount = 1f; TargetName.text = ""; TargetDistance.text = ""; TargetUI.GetComponent<CanvasGroup>().alpha = 0.2f; return; } TargetHealthBar.fillAmount = healthPercent; TargetDistance.text = string.Format( "{0}m", (int)Vector3.Distance(transform.position, PlayerShipAction.Target.transform.position)); }
private void GetNewTarget() { // TODO: This should be a "static" list modified on instantiation of new targetable objects. GameObject[] possibleTargets = GameObject.FindGameObjectsWithTag("CanTarget"); Vector3 reticulePos = Camera.main.ScreenToViewportPoint(_reticle.transform.position); Vector3 targetablePos; float shortestDist = Range; float currentObjectDistance = 0f; GameObject likelyTarget = null; for (int i = 0; i < possibleTargets.Length; i++) { targetablePos = Camera.main.WorldToViewportPoint(possibleTargets[i].transform.position); // Check if this object is in the screen. if (targetablePos.x > 0 && targetablePos.x < 1 && targetablePos.y > 0 && targetablePos.y < 1) { // Calculate screenspace distance from targeting reticule; currentObjectDistance = Vector3.Distance(targetablePos, reticulePos); // Check if this object is closer to the reticule than a previous object if (currentObjectDistance < Range) { likelyTarget = possibleTargets[i]; shortestDist = currentObjectDistance; } } } if (likelyTarget != null) { var destructable = likelyTarget.GetComponent<Destructable>(); Target = destructable; SetTargetUI(destructable); } }