/* * Check if this object meets all conditions. If yes, show arrow and update its position. * Else, hide arrow. * ***Technically, this checks the distance between this object and the center of the screen, ***not the player's actual position. */ void Update() { //Gets the center of the screen in world position Vector3 playerPos = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)); Vector3 objectPos = transform.position; //Gets how far the object is away from center of the screen. Vector3 heading = objectPos - playerPos; float distance = heading.magnitude; //Checks if this object: //1) Is close enough to be detected //2) Is actually outside the screen/not rendered by the camera //3) Is moving faster than the speed threshhold if (distance <= DetectionRange && !_renderer.isVisible && _rb2d.velocity.magnitude >= SpeedThreshhold) { if (transform.tag == "Enemy") { if (_playerWithinRangeScript != null) { _playerWithinRangeScript.activateCommentary(); _playerWithinRangeScript = null; } } _arrow.SetActive(true); UpdateArrow(); } else { _arrow.SetActive(false); } }
/* * Cache all needed components. */ void Start() { _renderer = GetComponent <Renderer>(); _rb2d = GetComponent <Rigidbody2D>(); _arrow = (GameObject)Instantiate(arrowPrefab); _arrow.SetActive(false); _playerWithinRangeScript = GameObject.FindGameObjectWithTag("CommentaryObject").GetComponentInChildren <PlayerWithinRangeOfEnemy> (); }