`
function Quake2()
{
var location1=transform.TransformPoint(5,5,5.0);
for(var any:GameObject in Vector3.Distance(location1,transform.Position))
{
any.gameObject.SendMessage(“GetTotalDmg”, 500);
}
}
`
Not sure what I did wrong with this code but my intention was to damage any objects within range.
Vector3.Distance returns a float, not a list of iteratable GameObjects! What you should do is iterate through ‘GetObjectsWithTag(“hitMe”)’ and use
if(Vector3.Distance(obj, position) < targetDistance)
{
// Deal Damage here!
}
Then, assign the tag “HitMe” to all the objects you want to be subject to this code.
My suggestion is to put every GameObject in a specific LayerMask and do a sphere cast at the center of the quake.
foreach(Collider c in Physics.OverlapSphere(position, radius, mask)
{
//deal damage here
}
That way you have absolute control over the objects that you are interested in (by setting the layermask) and let the physics engine do the job, instead of maintaining an array of objects yourself.