- Home /
Damage Within Range
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.
Answer by syclamoth · Sep 23, 2011 at 01:53 AM
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.
Did you even read my code? At no place and time did I try to set a Vector3.Distance equal to game objects... it's just used in a For loop o_O
For instance (for object in 5.0) but now that I think of it that's probly not even close to right which could be the problem.
Answer by Tommynator · Sep 23, 2011 at 02:18 AM
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.