How to find all GameObjects within a specific field of view/box

So I’m making a tower defense game and I’m attempting to make a mode where the user can actually control the aiming of the tower.

I currently have it set up so that the tower finds a gameobject, that gameobject becomes the target and then when it shoots a “bullet” there is a bullet script that just automatically follows the enemy until it hits it.

I’m having trouble thinking of how to do the aiming because it isn’t like any other FPS game where the bullet goes straight and just sees if it hits something.

I need to know how to find a target within like a 500 by 500 box for example.

So the logic behind what I want to do is "search for all game objects that are within the camera’s view inside a gui box that is 500px by 500px.

So for example:

12084-unityanswerspic1.png

From there I can just find the nearest.

So this is where I am at in terms of code:

                if(target==null){
					Collider[] cols=Physics.OverlapSphere(thisT.position, range, maskTarget);
					if(cols.Length>0){
						
						Unit currentTarget=null;
						Unit targetTemp=null;
						
						//we are in first person mode, and we are on manual, so the user is aiming, is he aiming at a target?
						if(firstPersonMode == _FirstPersonMode.Manual){
							
							
							
							float dist=Mathf.Infinity;
							Collider currentCollider=cols[0];
							foreach(Collider col in cols){
								float currentDist=Vector3.Distance(thisT.position, col.transform.position);
								if(currentDist<dist){
									if(col.bounds.Contains(HOW TO FIND THAT BOX I DREW)){
									    currentCollider=col;
									    dist=currentDist;	
									}
									
									
									
								}
							}
							//actually sets the current target
							currentTarget=currentCollider.gameObject.GetComponent<Unit>();
							if(currentTarget!=null && currentTarget.HPAttribute.HP>0) target=currentTarget;
							
							
						}
						
						}

Any information that might help me is appreciated.

Thanks for reading.

OK, then the answer(s) are simple. There’s a function where you can translate the camera viewport (forget the gui) into world coordinates. Unity - Scripting API: Camera.ViewportPointToRay

Just cast out a ray and instantiate an empty gameobect with a collider/trigger that will record when enemies enter.

HOWever, why do it this way? Why not set up a boxcollider w/ trigger around the tower in the first place and not worry about what view you are in.

NOTE: This is the answer for me, this is what I did and it achives exactly what I wanted.
I do want to point out it might not be the most effiecent, I say this because I haven’t tested anything else. If I didn’t already do this while waitting for an answer I would of used SinisterRainbow’s answer, so thanks. robertbu was also on to something.

THANKS ALL


THAT’S what I was looking for, I knew I could use rays but I didn’t know how. I’ve never used ray, just know about them.

I have however figured out something that works, already tested and what not.

So I added a box collider to my camera and I just activate it when I need to use it. Basically it’s acting as my “gui box” and if there is a enemy within it’s bound I am aiming at it.

So:
12090-unityanswerspic2.png

So as you can see, because my camera is a child of my tower it will follow it. And if there is an enemy within that box (the long one) it will mean "the user is aiming at it).

My code was easy to do as well:

//we are in first person mode, and we are on manual, so the user is aiming, is he aiming at a target?
						if(firstPersonMode == _FirstPersonMode.Manual){
							float dist=Mathf.Infinity;
							Collider currentCollider=cols[0];
							foreach(Collider col in cols){
								float currentDist=Vector3.Distance(thisT.position, col.transform.position);
								if(currentDist<dist){
									if(col.bounds.Intersects(Camera.main.collider.bounds)){
										currentCollider=col;
										dist=currentDist;
										//actually sets the current target
										currentTarget=currentCollider.gameObject.GetComponent<Unit>();
										if(currentTarget!=null && currentTarget.HPAttribute.HP>0) target=currentTarget;
									}
								}
							}

From a first person’s view it looks like:

12091-unityanswerspic3.png