1
\$\begingroup\$

I am working on a 2D Plattformer with melee attacks. The player is using a Kinematic Rigid Body and a Capsule Collider. Movement (even in slopes), jumping and and collission are working fine. My problem is that when swinging the axe I am animating a Box Collider 2D with IsTrigger set to true. But it is still not ignored and are pushing the player.

Without attack During attack

For movement and collissions I use the following code:

void Movement(Vector2 move, bool yMovement)
{
    float distance = move.magnitude;
    if(distance > minMoveDistance)
    {

        foreach (var item in hitBuffer)
        {
            if (item.distance > 0)
                Debug.DrawRay(item.centroid, item.normal, Color.red, 0.5f);
        }

        int count = rb.Cast(move, contactFilter, hitBuffer, distance + shellRadious);
        hitBufferList.Clear();

        for (int i = 0; i < count; i++)
        {
            hitBufferList.Add(hitBuffer[i]);
        }

        foreach (var item in hitBufferList)
        {
            Vector2 currentNormal = item.normal;
            if (currentNormal.y > minGroundNormalY)
            {
                grounded = true;
                if (yMovement)
                {
                    groundNormal = currentNormal;
                    currentNormal.x = 0;
                }
            }

            float projection = Vector2.Dot(velocity, currentNormal);
            if (projection < 0)
            {
                velocity = velocity - projection * currentNormal;
            }

            float modifiedDistance = item.distance - shellRadious;
            distance = modifiedDistance < distance ? modifiedDistance : distance;
        }
    }

    rb.position = rb.position + move.normalized * distance;
}

I suspect it might have some thing to do with the raycast being cast also from the animated box but I do not know if this is the case and how I could test it. I have setup a filter that I pass in to the rb.Cast but even with useTriggers set to false it does not make any difference.

Any feedback that could point me to in the right direction would be very helpful!

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

I solved the problem by not using RigidBody.Cast that uses all attached colliders but instead using a Collider.Cast so I only got back collission for that specific collider.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .