- Home /
Question by
FearofWonderland · Mar 08, 2014 at 11:19 PM ·
javascriptcollidertarget
Finding the collider of a variable object (Javascript)
So I want to use the collider of an object as a reference point as the actual transform would look out of place. This is the code I have to choose the object...
stockpileObject = GameObject.FindGameObjectsWithTag("Stockpile");
var closestStockpile : GameObject;
var distanceStockpile = Mathf.Infinity;
var positionStockpile = transform.position;
for(var goStockpile : GameObject in stockpileObject)
{
var diffStockpile = (goStockpile.transform.position - positionStockpile);
var curDistanceStockpile = diffStockpile.sqrMagnitude;
if (curDistanceStockpile < distanceStockpile)
{
closestStockpile = goStockpile;
distanceStockpile = curDistanceStockpile;
}
}
targetStockpile = closestStockpile;
}
And this is the code I have to get its collider and move an object towards it...
var stockpileCollider = targetStockpile.GetComponent(BoxCollider) as BoxCollider;
rotate = Quaternion.LookRotation(stockpileCollider.center - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, rotate, speed * Time.deltaTime);
transform.Translate(Vector3.forward * Time.deltaTime * speed);
I'm probably missing something really simple here but basically the issue is that the moving object that's supposed to be moving towards the target can't actually find the collider. Any help would be much appreciated as I'm not exactly the best at this kind of stuff.
Comment
Your answer
