Who you aiming that at? 🔫
To shoot an enemy, the enemy must be in the player's weapon 'collider'. Each weapon changes the collider's width, length, and height. Once inside, the player can shoot the enemy (basically) (there is also hit chances to consider as well as checking if the enemy is behind a wall). Without showing the player if they can shoot and hit an enemy, the player would be confused or not know how far you need to be to attack it. Of course, in the player's mind, if you’re using a Spear instead of your Fists as a weapon, the Spear's reach is greater than the player's Fists. If you’re using a long-ranged weapon, the end point of the weapon collider would be a guess by the player and having the player potentially waste ammo by trying to shoot something they cannot hit is a situation best avoided.
Currently the UI Aim Indicator (TM - *Ding sound*), Shows objects that can be hit by the player's attack. Right now, that includes enemies, as well as barrels (💥explosive and inexplosive!🛢️). In the future this might include other interactable objects such as doors or items, as well as an objective marker.
When an enemy has entered the (invisible) weapon collider, an icon class is created and added to a list (of Icons). The Icon class contains; the RectTransform of an icon marker, the RawImage component of that said marker, a reference to the enemy that entered the weapon's collider, as well as the enemy’s position. It then calculates the direction and angle to the enemy and shows the icon on the Aim Indicator UI bar relative to the player's position to the enemy.
If the enemy exits the weapon collider, the enemy is removed from the icon list (icon.enemyObject = null) and the icon disappears. There is also a check to see if the player can ‘spot’ the enemy inside the collider. This is done with a Raycast from the player to each enemy in the collider. If the enemy is behind a wall but also inside the collider, the enemy icon is disabled. But when the Raycast finds the enemy, the icon is then enabled again. This is done with another List<> that contains all the objects that are seen.
Icon classes only get created if all the icon classes in the list are already being used by an enemy. So, if an already created icon class is not following an enemy and an enemy enters the weapon collider, instead of making a new Icon class, it uses the unused one. If all Icons are following enemies, then it creates a new Icon Class to add another marker to the UI Aim Indicator. (A lot is going on here! 🥵)
Show Icon Function
There's also a check to see if the player is looking at a barrel or an enemy. This changes the RawImage sprite of each marker to either a picture of a barrel or a red diamond indicator.
But wait, that's not all! 👨💼💰💹 Each weapon has a specific number of enemies that it can aim at a given time. Fists, Spear, Spine, and Bible weapons can only aim at one enemy at a time. They all attack the nearest object to the player. So, the UI Element removes all the other icons on the Aim Indicator if they are in the icon list. The only icon marker remaining is following the object that is nearest to the player. The Shotgun aims at all enemies in the collider that the player can see, and the Machine Gun aims at 4 objects at most, removing other icons of enemies that cannot be hit. (a whole lot going on in the script! - hold on... I need a drink... 🍹... ahhh...)
Ok... so where was I... Oh, right - The icons on the UI bar also take into consideration the distance between the player and the object the icon is following. The further away from the player the object in the world is, the smaller the icon is on the screen (the closer the object, the larger the UI icon is). This helps somewhat to recognize what the player is aiming at. 🎯