Hi,
I’m making a tower defense game and I came to a problem concerning the range of fire of my turret.
Turret are made children of a tile map. The map is a neast a gameObject and the turret is the last children of a gameobject hierarchy (5 parents). In thoses parents 3 have a custom scale to make the map good looking. These are the scales : (1;1;1) => (5;1;5) => (1;1;1) => (0.55;2.1;0.55) ==> (0.5;0.8;0.5)
Resulting the scale of the turret should be : (1510.550.5 ; 1112.10.8; 1510.550.5) => (1.37; 1,68 ;1.37).
Now my problem is that the turret have a sphere collider attached to detect enemies, and the sphere is modify by the combined parents scales. Would not be a problem if I didn’t add teleporter to my gameplay.
As the enemys advance they colldier with teleporter wich teleport them to another map section.
As I use agent.Warp so the OnTriggerExit is not call and I don’t know when the enemies exit my range.
To solve that I chose to check the distance within the tower and the enemis which enter the range.
To do that I used this piece of code :
for (int i = 0; i < enemiesList.Count; i++)
{
float distance = (enemiesList[i].gameObject.transform.position.x - this.transform.position.x)* (enemiesList[i].gameObject.transform.position.x - this.transform.position.x) + (enemiesList[i].gameObject.transform.position.z - this.transform.position.z)* (enemiesList[i].gameObject.transform.position.z - this.transform.position.z);
Debug.Log("enemiesList[i] :" + enemiesList[i].name + " distance :" + distance + " deltax : " + enemiesList[i].gameObject.transform.position.x + "-" + this.transform.position.x + " delta y : " + enemiesList[i].gameObject.transform.position.z + "-" + this.transform.position.z);
if (distance > collider.radius * collider.radius)
{
enemiesList.Remove(enemiesList[i]);
i--;
}
}
Problem : collider.radius if false due to the combined parents scale.
I try to calculate it using the LoosingScale but there is the this.transform.lossyScale;
the lossyScale calculated by the engine is far different from the one I suppose is.
So I tried to see what could be the real lossy scale by comparing the actual sphere collider range to a simple sphere in the scene:
The tower sphere (gizmo in the screenshot) is a 6 radius sphere * scale vector
The blue sphere is a sphere scaled to (14.1; 14.06;14.1) to match the tower sphere.
equivalent to a sphere collider with a radius of 6 scaled to (1.17;1.18;1.17).
Seem to match cause 12*1.17 = 14.04
In conclusion loosyScale cannot be use to know the real/global radius of a sphere but now I have a bigger problem cause I cannot the real distance corresponding to the sphere radius.
As my tiled have all the same size and hierarchy I added 20% to the radius. This work for this case but i’m wandering if there is a way to solve it properly??
I Hope it’s understandable and I thanks you for your help.