- Home /
Check Radius Problem
Hello Everybody,
I need your little help. I have a script I would check around the character and i found the closest enemy with this script. This script working good but this script check only closest enemy when character is spawn how can i check it everytime ? Example how can i check my radius when my chracter is walking
My Code is here ;
private var startPoint: ClosestEnemySystem; function Start(){ ClosestEnemy(); startPoint= ClosestEnemy().GetComponent(ClosestEnemySystem); //Wonder= ClosestEnemy().GetComponent(ClosestWonderSystem); startPoint.isStart=true; }
function ClosestEnemy() : GameObject {
var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Soldier"); var closest : GameObject; //var distance = Mathf.Infinity; var distance : float; distance = 30;
var position = transform.position;
for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; } function OnDrawGizmos () { if (startPoint != null) { Gizmos.color = Color.red; Gizmos.DrawLine (transform.position, startPoint.gameObject.transform.position); } }
Answer by Badger · Aug 10, 2010 at 10:14 AM
Are you sure there are enemies within 30 units? If not, closest will not be initiated by this imho.
Also, why would you want to replace Infinity with a set distance? It doesn't reduce the number of loops on this function, as it still loopts through all enemies doing a distance calculation. Might be a solution to check in the end if the distance is smaller than 30, if not, set closest to player position (or whatever else you'd like) :)
Thank you for your reply but i developing a 3d tower defence game i controlling my characters radius and if a object in my radius i will attack this object. If i want make a # stable tower it is no problem but But my characters is walking :) How can i control my radius when my character when they walking ?
I see, i reread the question.
Put the call to ClosestEnemy() in the update function ins$$anonymous$$d of the start one. That should have it checked every time around i think
I think Badger spotted the basic problem. I didn't realize that you were only calling the ClosestEnemy method in Start(). You need to put logic somewhere (such as Update) that gets called more than once. Or, switch to colliders and the physics loop will do that for you.
Answer by Bampf · Aug 10, 2010 at 11:06 AM
I can't spot anything wrong with your code off the top of my head. Is that script attached to the moving game object?
You should consider using sphere colliders instead. They generally require a lot less code, and you can see them in the Unity editor window so it's very easy to visualize when things collide.
As a bonus, the physics engine is optimized to avoid doing things like test every object in the scene the way your loop has to.
You can also use raycasting with the colliders, so you can do things like figure out if a laser beam hits an enemy, or if the enemy can see the tower behind another object.
Thank you for your reply i draw gizmos with this code but script draw gizmos only when object spawned :(
function OnDrawGizmos () { if (startPoint != null) { Gizmos.color = Color.red; Gizmos.DrawLine (transform.position, startPoint.gameObject.transform.position); }