Here’s the answer as proposed by @DMGregory.
The key’s to make use of the collider itself for the .Cast
as an alternative of the rigidbody
Old code – irrelevant elements omitted
non-public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
collider = GetComponent<Collider2D>();
}
non-public bool HasCollision(Vector2 course)
{
int rely = rb.Cast(
course,
movementFilter,
collisions,
collisionOffset
);
return rely > 0;
}
New code – irrelevant elements omitted:
non-public Rigidbody2D rb;
non-public Collider2D collider;
void Start()
{
rb = GetComponent<Rigidbody2D>();
collider = GetComponent<Collider2D>();
}
non-public bool HasCollision(Vector2 course)
{
// Here's the important thing change!
int rely = collider.Cast(
course,
movementFilter,
collisions,
collisionOffset
);
return rely > 0;
}
This works, nevertheless it’d be nonetheless nice to listen to a couple of solition solely based mostly on layers. The usecase could be to permit for composite shapes with out resorting to one thing like a foreach
the place each step within the iteration does a .Cast
.