- Home /
Overlap sphere radius problems
I have a little melee function in the code below. The problem is that for some reason the radius of the overlap sphere doesnt seem to work properly, a radius of 1 seems to encompass an area far far greater(more like 10). Secondly nothing actually happens if the object you are trying to melee is at point blank range.
With the dist < Range the issue of limiting your own melee range seems to be fixed but that hardly seems ideal, otherwise whats the point of the radius? And the issue of a point blank attack is still a problem.
Using Gizmos.DrawWireSphere the overlapsphere appears like it should be in the correct place, just I have no idea why it doesnt function as I thought it should
var colliders = Physics.OverlapSphere (transform.position, 1f);
foreach (var hit in colliders)
{
Vector3 dir = (hit.transform.position - transform.position).normalized;
var direction = Vector3.Dot (dir, transform.forward);
if (direction < MeleeDirection)
{
continue;
}
float dist = Vector3.Distance(hit.transform.position, transform.position);
if (hit.rigidbody && dist < Range)
{
HealthManager targetHealth = hit.transform.GetComponent<HealthManager>();
if (targetHealth)
{
targetHealth.TakeDamage (damage, tr.forward);
hit.rigidbody.AddForce (300, 300, 300);
TNManager.Create(gibsPrefab, hit.transform.position, hit.transform.rotation);
}
}
}
Answer by getyour411 · Apr 14, 2014 at 02:28 AM
Do your enemy units have a large sphere or other collider, like maybe an awareness collider or something like that, which might be within the OverlapSphere? OverlapSphere is used frequently so I have to assume any issues you are experiencing are unique to your setup.
Also, what's this for?
if (direction < MeleeDirection)
{
continue;
}
In your 'point blank' issue, using debug, what is the value of direction and MeleeDirection?
Ahh the enemy units do have an awareness collider(a big old sphere collider) on em in addition to their capsule collider I completely forgot about! I also sorted the point blank issue by making the other colliders slightly larger, seems to work now so I guess I can consider that part fixed as well, thought Im not sure why that sorts it out.
The part you mentioned is just for a fov type of effect, ignore colliders outside of a value so the overlap melee sphere acts as a sort of melee cone.
Thanks! I dont know how to mark this as resolved(is there a way to do that?).
Your answer
![](https://koobas.hobune.stream/wayback/20220613142644im_/https://answers.unity.com/themes/thub/images/avi.jpg)