- Home /
Question by
JPB18 · Dec 08, 2012 at 02:12 PM ·
javascriptgameobjectfunctionlagfind
Function to locate game object with smaller distance to target makes the game lag - UnityScript
Hi there!
So, I've done this function so the script would discover which of a group of certain gameObjects (each representing a weapon) would be closer to the target. However, every time I fire the weapon, the game begins to lag (FPS drop from 63 to 0.3). Is there any recomendation to improve it? Here it is:
function CheckClosestWeapon (weaponTag : String, parent : Transform) : GameObject
{
var closest : GameObject;
for (var weapon_go : GameObject in GameObject.FindGameObjectsWithTag(weaponTag))
{
if (closest != null)
{
if (weapon_go.transform.parent.parent.transform == parent)
{
var distance1 = Vector3.Distance(weapon_go.transform.position, target.position);
var distance2 = Vector3.Distance(closest.transform.position, target.position);
if (distance1 < distance2)
{
closest = weapon_go;
}
}
}
else
{
if (weapon_go.transform.parent.parent.transform == parent)
{
closest = weapon_go;
}
}
}
return closest;
}
Thanks in advance...
Comment
Best Answer
Answer by JPB18 · Dec 09, 2012 at 01:43 PM
Fixed the issue by adding #pragma strict at the top...