- Home /
Weird behavior finding target using Physics.OverlapSphere
I'm trying to learn Unity and making a simple Tower Defense game to do it. The towers have a radius in which they can attack. I'm using Physics.OverlapSphere to find available targets and then checking which target is closest. That target becomes the target until he leaves the tower's range, after which it reruns the function to find a new target.
Almost everything seems to be working properly. I'm using gizmos to visually display the target and that works great. The only thing that isn't working is I want the tower to LookAt the object. If I set the tower attack radius (detectionRange) to a high number like 20 or more which takes up most of my map, the tower will rotate properly only when the targets get really close (5 or less). If the radius is more reasonable like 5, then the gizmos and targeting works but the tower doesn't rotate.
What am I doing wrong?
Here's the code (also since I'm learning, please feel free to tell me where I could do things better):
#pragma strict
var hasTarget : boolean = false;
var detectionRange : float;
private var closestEnemy : Transform;
function Update () {
//Makes sure we have a target
if (hasTarget){
//Checks the distance between the tower and target
var currentEnemyDist : float = (closestEnemy.position - transform.position).sqrMagnitude;
//If the target is further than the detectionRange, get a new target
if (currentEnemyDist > detectionRange) {
hasTarget = false;
}
}
//If we have a target, rotate to look at it
if(hasTarget){
transform.LookAt(closestEnemy.transform);
}
//If we don't have a target, find one
else {
GetTarget();
}
}
function GetTarget() {
//Create a sphere with radius of detectionRange and return targets in range
var enemiesInRange : Collider[] = Physics.OverlapSphere(transform.position, detectionRange);
var enemyDistance : float = Mathf.Infinity;
var colDistance : float;
//Cycle through each target in the sphere
for (var col : Collider in enemiesInRange) {
//Targets must have the tag "Enemy"
if (col.gameObject.tag == "Enemy") {
//Check the distance from the tower to the target
colDistance = (col.transform.position - transform.position).sqrMagnitude;
//Finds the closest enemy
//Checks each target to see if the next one is closer than the previous
if(colDistance < enemyDistance) {
//If it's closer, set the target
closestEnemy = col.transform;
//Set the distance to check against subsequent potential targets
enemyDistance = colDistance;
//Set the tower to know it has a target
hasTarget = true;
}
}
}
}
function OnDrawGizmos () {
Gizmos.color = Color.blue;
if(hasTarget) {
// Draws a blue line from this transform to the target
Gizmos.DrawLine (transform.position, closestEnemy.position);
}
//Draws a sphere to see the detectionRange
Gizmos.DrawWireSphere (transform.position, detectionRange);
}
Answer by Berenger · Jan 13, 2012 at 02:25 AM
Vector3.Distance(A, B) instead of (A - B).sqrMagn seems to solve the problems when I use your script. As to know why ... [EDIT] Ok, I think when you want a detectionRange of 10, because of .sqrMagnitude, you detect only the enemies at sqrt(10).
Looks like that worked. I was using sqr$$anonymous$$agn from another script I found here but I was a little confused as to it's exact function. Thanks.
Your answer
Follow this Question
Related Questions
Trying to get an object to "LookAt" the first collided object(Physics.OverlapSphere) 0 Answers
[RePost for No Answer] How to make a character look at the direction it's going towards 1 Answer
replace raycast with transform direction 1 Answer
Indicator rotating around player 1 Answer
look at player when button pressed 1 Answer