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:
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.