- Home /
How is OverlapSphere used?
I've only found a few posts about it, but i haven't seen it USED, only declared in a var. var EG colliders = Physics.OverlapSphere( position, 150.0);
So how exactly do you put this to use?
Answer by reissgrant · Feb 01, 2011 at 12:40 AM
This was posted by "Duck" a while back: Click Here
*If your enemies have colliders attached, you can use Physics.OverlapSphere to quickly get your initial list of enemies within a certain radius.
You can then loop through each enemy within this list and determine the relative distance to the player, and apply some level of damage to each enemy within the radius, proportional to its distance.
It would end up looking something like this. It assumes that your enemy objects have both a collider and a script called "Enemy" attached.*
function AreaDamageEnemies(location : Vector3, radius : float , damage : float ) { var objectsInRange : Collider[] = Physics.OverlapSphere(location, radius); for (var col : Collider in objectsInRange) { var enemy : Enemy = col.GetComponent(Enemy); if (enemy != null) { // linear falloff of effect var proximity : float = (location - enemy.transform.position).magnitude; var effect : float = 1 - (proximity / radius);
enemy.ApplyDamage(damage * effect);
}
}
}
Answer by Jessy · Feb 01, 2011 at 12:41 AM
The potential uses, like for most everything in the Unity API, are infinite.
Let's see what comes off the top of my subconscious...
An enzyme is in a stomach. Check to see if certain proteins are within range of the enzyme. Destroy the colliders for the proteins that overlapped because now they're more gooey and ephemeral, instead of being solid gelatinous globs.
That ship from The Abyss starts rising at the end of the movie. Check the sphere around it, and if any dolphins are within range, have them freak the @$#% out. As in, maybe use Collider.attachedRigidbody along with Rigidbody.AddExplosionForce.
Like the docs say, it's dealing with bounding boxes, so it could result in more useful behavior sometimes, than just checking distances between transform.positions, as distance checks often do.
Haha, thanks for the eye opener but "how it's used" is a bit different then "how to use it" ;) But I might use that advice later for my rising-ship-freak-the-@#$*&-out-dolphin game I've been planning :P
Your answer
Follow this Question
Related Questions
Physics Overlap Sphere issue 0 Answers
OverlapSphere is really confusing 0 Answers
Is Raycast relatived to screen resolution? 1 Answer
How to use Sphere colliders to detect the "Player" 1 Answer
Collision between player and a ball 1 Answer