I would appreciate it if you could help to untie this knot of 2D Physics…
I’m unsuccessfully trying to build a Physics2D.OverlapCircle enemies detection system for my attack system…
This is my situation:
Lots of enemies and one melee attack system that has to use the function SendMessage (“Damage”).
I know that there is the “trigger on weapon” way but I started with an Aldonetto suggestion and now I want to understand this Physics2D.OverlapCircle…
This is what I’ve made:
#pragma strict
var playerHealth : float = 100;
var playerDamage : float = 25;
var playerRange : float = 5;
var center : Vector2 = transform.position;
var radius : float = 5;
//Attack () is called by the touch of the attack button
function Attack ()
{
Enemycheck ();
}
function Enemycheck (center: Vector2, radius: float)
{
var hitColliders = Physics2D.OverlapCircle(center, radius, 1 << LayerMask.NameToLayer("Enemies")); //layermask to filter the varius useless colliders
for(var i : Collider2D in hitColliders) //here the problem
{
var distance = Vector2.Distance(hitColliders.transform.position - transform.position);
if (distance <= playerRange)
{
hitColliders.SendMessage("Damage", playerDamage); //send message damage + damage variable
}
}
}
Take a look to the line 20.
I tried with Aldonetto’s way, he use “in” operator that returns true if the specified property is in the specified object.
I tried to translate the 3D example of the documentation but they use hitColliders.Length that doesn’t work with 2D Collider…
I hope that you’ll try to let me understand and if you require any further information about the script or other things, feel free to reply!
function Attack ()
{
var hitColliders : Collider2D = Physics2D.OverlapCircleAll(transform.position, enemyAttackRange, 1 << LayerMask.NameToLayer(“Player”));
for (var i = 0; i < hitColliders.Length; i++)
{
//hitColliders*.SendMessage("Damage", enemyDamage);*
if(playerInAttackRange ==true)*
{*
if(Time.time > nextAttack)*
{*
nextAttack = Time.time + enemyAttackTime; *
_ hitColliders*.SendMessage(“Damage”, enemyDamage, SendMessageOptions.DontRequireReceiver);_
_// Debug.Log(“Damage sent”);_
_ } _
_ }_
_ }_
_}*_ Here the solution that I’ve found by myself! However thanks
See Unity - Scripting API: Physics2D.OverlapCircle. It only returns one collider, the one with the lowest Z. It is different than in 3D, where OverlapSphere returns an array! So you can’t use the same code. Sorry.
EDITED: using OverlapCircleAll (sorry I didn’t see that function!) this piece of JS code should work fine then:
for (var i = 0; i < hitColliders.Length; i++)
{
var distance = Vector2.Distance(hitColliders*.transform.position - transform.position);*
Note that you have mixed “in” operator with arrays in your first piece of code. You used “in” with “i” variable, but then you didn’t reference “i” inside the loop code. The code I wrote up there is using arrays directly (it’s more simple). But if you really wanted to use “in”, then inside the “for” code you need to use “i” instead of “hitColliders”. “hitColliders” is the entire array; “i” is one element for each of the iterations. That’s where the problem resides.
Take a look to the range 20. I tried with Aldonetto’s way, he use “in” owner that profits real if the specified residence is in the specified item. I tried to convert the 3D example of the certification but they use hitColliders.Length that does not perform properly with 2D Collider… Spybubble