c# - Unity3D Physics2D.OverlapAreaNonAlloc strange (bug?) behaviours with layers -
i have encountered strange bug physics2d.overlapareanonalloc , created test project confirm it.
scene : 2 cube on top of each other, rigidbody2d , box collider, bottom 1 kinematic. top 1 "player".
i wanted use physics2d.overlapareanonalloc enemy detection used code looks this, attached player :
using unityengine; using system.collections; public class test : monobehaviour { vector2 pointa; vector2 pointb; collider2d[] enemies = new collider2d[2]; void update () { //enemy detection pointa = (vector2)gameobject.transform.position - new vector2(0.1f, 0f); pointb = (vector2)gameobject.transform.position + new vector2(0.1f, 0.1f); if (physics2d.overlapareanonalloc(pointa, pointb, enemies, 9) > 0) { debug.log("first enemy :" + enemies[0].name); } } }
9 layer number of enemies layer. there no enemies in scene previous code should never display in console. sadly...
when run code, here happens : - if "player" layer default, somehow detected physics2d.overlapareanonalloc - if "player" layer layer, not detected.
i don't understand behaviour, "default" supposed "super" layer contains layers ?
use layermask.getmask() in call physics2d.overlapareanonalloc().
// "enemy" represents layer assigned enemies physics2d.overlapareanonalloc(pointa, pointb, enemies, layermask.getmask("enemy"))
Comments
Post a Comment