RigidBody2D.Cast Returns triple the values

Hello, I have a problem with a simple cast on one of my scripts

count = myRigidBody.Cast(new Vector2(0, 0), deathCheckHitBoxFilter, hitBuffer);

this returns 3, all the hitBuffer_.transform.name return the same object name, a single empty Gameobject with only a box collider I made just to test it_
7518554--927536--Fir.PNG
but this
csharp*_ <em>*count = collisionRigidBody.Cast(new Vector2(0, 0), deathCheckHitBoxFilter, hitBuffer);*</em> _*
returns 1 and again the single hitBuffer_.transform.name give the same object
7518554--927533--First.PNG
So now what is the difference??
The first RigidBody is attached to the player
7518554--927524--First.PNG
The collisionRigidBody is attached to a gameObject that is a child of the player
7518554--927527--First.PNG
The code looks like this and the results come from the first frame of the game
```csharp_

*int count = 0;
count = myRigidBody.Cast(new Vector2(0, 0), deathCheckHitBoxFilter, hitBuffer);

        print("Check for death Count is " + count);

        for (int i = 0; i < count; i++)
        {
            print(hitBuffer[i].transform.name);
        }*</em></em></em>

```
I can provide extra info if you need

Do you have 3 colliders on the first Rigidbody2D and only 1 on the second? The docs make it sound like it separately casts each individual collider on the Rigidbody2D in the direction you specify.

https://docs.unity3d.com/ScriptReference/Rigidbody2D.Cast.html

1 Like

As above but also why are you using a degenerate direction of 0? This is like devs who do raycast with zero direction instead of OverlapPoint which is super fast compared to casting.

If you want to check overlaps for colliders either individually or all attached to a Rigidbody2D then use OverlapCollider.

1 Like

No only one but I have 2 children with only Box Colliders on them so the cast must have taken them, I manged to solve it by adding static RigidBodies to the other children

tbh I never knew this existed I will change my code right away thank you so much for the info :smile:

Well they are attached to the Rigidbody2D so yes, they will be cast because that’s what doing a Rigidbody2D cast does as per the docs.

Always worth just browsing the API docs of the main components. Some very useful stuff in there!

1 Like