Use a trigger: child to the turret an invisible object with a capsule collider (uncheck the Mesh Renderer component to make it invisible), adjust it’s radius and height to the volume you want to detect and set Is Trigger, then in the OnTriggerStay compare the tag to learn if it’s an enemy. If it is, cast a ray to its direction to check if the object is visible:
function OnTriggerStay(col: Collider){
if (col.tag == "enemy"){
if (Physics.Raycast(transform.position, col.transform.position-transform.position, detectionRadius){
// enemy at sight: do whatever you want
}
}
}
If the air radius must be much larger than the land radius, repeat the trick: use another trigger to detect it, but ignore the enemy if its position.y is below some height.
and as for the Air Targets and Ground Targets simply have a set range for Air like 100-300 it can hit Air and below 100 it can only target ground which would simply just be the guns second distance check 0-100.
I have a some code somewhere if you like to see an example of how to target something at a distance.
Actually the best way would be to create an array of gameojects and find the closest one to the turret then assign the target to the closest I have a video where I have a turret that fires at closest target:
Youtube : Multi-Targetting Turret - YouTube