#How does BoxCollider2D.Cast work?

1 messages · Page 1 of 1 (latest)

proud laurel
#

I am trying to set up "my own collision engine" by using Unity logic just as helpers. I have two distinct game objects, they are both set up with components:

  • BoxCollider2D (Is Trigger)
  • Rigidbody 2D (Dynamic, simulated, discrete, start awake)

I was under the impression that if I were to run BoxCollider2D.Cast, passing the default layer they are both in, I would get the other object in the list of hits. However the list of hits always just returns the floor object (which has the PlatformEffector2D component).

Isnt the point of Cast to return other GameObjects?

Small Snippet where the problem is going on:

⁨```csharp
private bool Move(Vector2 directionAxis, float force, out RaycastHit2D hit)
{
hit = default;

Vector2 dir = force > 0 ? directionAxis : directionAxis * -1f;
float dist = Mathf.Abs(force);

int count = col.Cast(dir, filter, hits, dist + SKIN_WIDTH);

if (count > 0)
{
    hit = hits[0];
    transform.position += (Vector3)(dir * (hit.distance - SKIN_WIDTH));
    return true;
}

transform.position += (Vector3)directionAxis * force;
return false;

}

public bool MoveHorizontal(float forceX, out RaycastHit2D hit)
{
return Move(Vector2.right, forceX, out hit);
}

#

hits is a
⁨```csharp
private RaycastHit2D[] hits = new RaycastHit2D[8];


which is always empty aside from [0] which is the floor (while im walking arround)
frail sierra
#

Casts interact with colliders

#

not with Rigidbodies

#

but also

proud laurel
#

well, they still are not interacting with my boxcollider

frail sierra
#

Cast on a particular BoxCollider ONLY casts against that particular collider

#

actually sorry

#

I have that backwards

#

that's how it works in 3d

proud laurel
#

I was following this:

frail sierra
#

Did you read the docs for this?

#

Yeah but also:

Casts the Collider shape into the Scene starting at the Collider position ignoring the Collider itself.

#

it ignores itself

proud laurel
#

Thats fine

frail sierra
#

so what object are you expecting it to hit that it isn't hitting

proud laurel
#

Look, my two objects are colliding right now, as you can see by the green outline (they are selected in the Hierarchy tab, which makes the collider outline get drawn)

Also, they panel in the right is showing the props for both the selected elements, so you can see they are both "Is Trigger"

#

The result of cast is:

frail sierra
#

what filter are you using

proud laurel
#

all elements are null, and the first one is the floor

#

ContactFilter2D

frail sierra
#

yes... of course

#

I mean

#

how is it configured

proud laurel
#

CollisionMask is setup via the editor to both point at Default

#

this is Awake

frail sierra
#

useTriggers = false;

proud laurel
#

lol

frail sierra
#

you just said they are trigger colliders

proud laurel
frail sierra
proud laurel
#

Thanks

frail sierra
#

but also:
filter.SetLayerMask(CollisionMask);⁩ will also automatically handle the ⁨filter.useLayerMask = true;⁩ part

proud laurel
#

You cured my blindness