public static void TurnOffLockOn() { LockedOn = false; _image.enabled = false; LockedEnemy = 0; _target = null; }
void Update() { //Press Space Key To Lock On if (Input.GetKeyDown(KeyCode.Space) && !lockedOn) { if (nearByEnemies.Count >= 1) { lockedOn = true; image.enabled = true; //Lock On To First Enemy In List By Default lockedEnemy = 0; target = nearByEnemies[lockedEnemy]; } } //Turn Off Lock On When Space Is Pressed Or No More Enemies Are In The List else if ((Input.GetKeyDown(KeyCode.Space) && lockedOn) || nearByEnemies.Count == 0) { lockedOn = false; image.enabled = false; lockedEnemy = 0; target = null; } //Press X To Switch Targets if (Input.GetKeyDown(KeyCode.X)) { if (lockedEnemy == nearByEnemies.Count - 1) { //If End Of List Has Been Reached, Start Over lockedEnemy = 0; target = nearByEnemies[lockedEnemy]; } else { //Move To Next Enemy In List lockedEnemy++; target = nearByEnemies[lockedEnemy]; } } if (lockedOn) { target = nearByEnemies[lockedEnemy]; //Determine Crosshair Location Based On The Current Target gameObject.transform.position = cam.WorldToScreenPoint(target.transform.position); //Rotate Crosshair gameObject.transform.Rotate(new Vector3(0, 0, -1)); } }
// Update is called once per frame void Update() { if (VisibleEnemies.Count == 0) { TurnOffLockOn(); } else if (Input.GetKeyDown(KeyCode.Tab) && !LockedOn) { if (VisibleEnemies.Count > 0) { LockedOn = true; _image.enabled = true; LockedEnemy = 0; _target = VisibleEnemies[LockedEnemy]; } } else if ((Input.GetKeyDown(KeyCode.X) && LockedOn)) { TurnOffLockOn(); } if (Input.GetKeyDown(KeyCode.Tab)) { if (LockedEnemy == VisibleEnemies.Count - 1) { LockedEnemy = 0; _target = VisibleEnemies[LockedEnemy]; } else { LockedEnemy++; _target = VisibleEnemies[LockedEnemy]; } } if (LockedOn) { _target = VisibleEnemies[LockedEnemy]; Vector3 temp = gameObject.transform.position = _cam.WorldToScreenPoint(_target.transform.position); if (temp.z < 0) { temp *= -1; // Needs this to prevent drawing crosshair mirrored duplicate in the opposite direction } gameObject.transform.position = temp; gameObject.transform.Rotate(new Vector3(0, 0, -1)); } }