Allowing the turret to move, yet be restricted via rigidbody and transform constraints and managing. On update the transform of the player turret is updated to the mouseY and mouseX variable. The minimum and maximum transform clamp prevents object clipping and unrealistic angles.
Firing the turret/guns:
Firing a raycasted shot when mouse 0 input is detected. Whether this be a single click or holding down the button. This raycast then determines if it hit an enemy prefab, if so it then causes damage to that instance.
Enemy spawning and movement:
Enemy spawning is controlled by a spawn container in which a timer instantiates the enemy prefab which then targets an arbitrary building. To avert errors when a building is destroyed a Try Catch is called within update to check if the currently targeted building still exists within the scene. If not, then the enemy object targets a new existing building to attack.
Pickups and turret firing power:
The turrets firing speed is controlled through an integer, this variable can be changed at any moment to reflect a power up that the player has collected. Here the custom event called “FireRateUp” sets the fire rate of the players turret for 10 seconds after the player has successfully hit the falling powerup.