Collider gets stuck in floor.. Research has been fruitless

My floor is a collider, my player is a collider. On jump, the player reaches apex and downward velocity is applied to simulate a real jump. Sometimes, when the player lands, his collider ends up in the floor collider and the player cannot move. I’ve tried setting kinematic = true when the player almost reaches the floor, checking with a ray before the player lands setting velocity.Y to 0, nothing works, player still penetrates floor which is annoying.

My current endeavor is trying to detect OnCollisionEnter and tag = floor then get bounds of floor and get the top, and set the players foot above that point…

Is this a good way to go about this, and how the hell would I code that?

Have you tried changing the interpolate options in the Rigidbody. Also might be moving to quickly try playing with the PhysicManager settings in the Edit->Project Settings menu. Also what type of collider are you using on your player.

1 Like

Extrapolate seems to help, but the issues still occurs which as you know means it didn’t work. I tried to adjust the gravity but I liked the gravity at -9.81.

[quote=“image28_1, post:2, topic: 629016, username:image28_1”]
Also what type of collider are you using on your player.
[/quote] I’m using Box Collider.

Not sure why this is an issue. In real life you don’t get stuck in floors. I wish there were a way to set collider density and hardness so this kind of thing could be avoided.

2656491--187287--Capture.PNG

Perhaps the issue is his animation. The player apparently rocks back and forth while running, maybe this diagonal rotation to the collider is wedging into the floor??? Not sure

2656491--187288--Capture2.png

The code that brings player back to Earth checks if player isn’t grounded and time in air is greater than maxTimeinAir then Add Force downwards * X every update till grounded. I’ve since only applied this force once and the issue remains. I even cast ray down and if hit within .3f distance then set rigidbody velocity y axis to 0… no cigar

Why not have circle collider for feet? Maybe even a capsule collider, but sadly there isn’t one for 2D. This collider shouldn’t be swaying with animation, but look like sliding along the ground.

1 Like

I think colliders no matter what type will have the same issue. I tried to detect OnCollisionStay if player is in floor using bottom of player collider < top of floor collider then add -.1f to the Y position of player, but I can’t get the correct calculations apparently because the player is in the floor and my breakpoint isn’t hitting. What am I doing wrong?

    protected void OnCollisionStay(Collision contact)
    {
        GameObject contactObj = contact.gameObject;

        float length = contactObj.transform.localScale.x * ((BoxCollider)contactObj.GetComponent<BoxCollider>()).size.x;
        float width = contactObj.transform.localScale.z * ((BoxCollider)contactObj.GetComponent<BoxCollider>()).size.z;
        float height = contactObj.transform.localScale.y * ((BoxCollider)contactObj.GetComponent<BoxCollider>()).size.y;
        Vector3 dimensions = new Vector3(length, height, width);
        float topCollider = contactObj.transform.position.y + dimensions.y / 2;

        float lengthPlayer = transform.localScale.x * ((BoxCollider)GetComponent<BoxCollider>()).size.x;
        float widthPlayer = transform.localScale.z * ((BoxCollider)GetComponent<BoxCollider>()).size.z;
        float heightPlayer = transform.localScale.y * ((BoxCollider)GetComponent<BoxCollider>()).size.y;
        Vector3 dimensionsPlayer = new Vector3(lengthPlayer, heightPlayer, widthPlayer);
        float bottomPlayer = transform.position.y - dimensions.y / 2;

        if (contact.gameObject.GetComponent<MovingObject>() != null)
        {
            MovingObject movingContact = contactObj.gameObject.GetComponent<MovingObject>();
            if (movingContact.tag == "Enemy")
            {
                if (movingContact._attackCoolDownTimer == 0)
                {
                    CauseDamage(movingContact);
                    movingContact._attackCoolDownTimer += 1;
                }
                else
                {
                    movingContact._attackCoolDownTimer -= 1;
                    if (movingContact._DamageFrequency == movingContact._attackCoolDownTimer)
                        movingContact._attackCoolDownTimer = 0;
                }
            }
        }
        else if (contactObj.tag == "Floor")
        {
            if(bottomPlayer < topCollider)
            {

                bool inFloor= true;

            }
        }
    }

inFloor isn’t hitting when my player is clearly in the floor…?

Actually, capsule colliders for feet worked, Nice! Thanks Zalfis!

1 Like

I think the real problem here may be that you are assigning a velocity, so the next frame will pretty much always have a penetration amount (depending on max depenetration velocity). If you use forces this may be less visible. How are you moving it?

As I said, when jump time in air > max time in air (apex), begin applying downward force with AddForce (-Vector3.up, 50f) in update context. I even tried casting ray down .3f and if the floor is hit set velocity.y to 0. Did not worko… The player would stop an inch above the ground and float down the rest but even then sometimes he’d penetrate

Just Checking you have your physics code in FixedUpdate and not Update right?

This is something I looked at and yes it’s in fixed update. I call ReceiveControl in fixed update in the class player which derives from movingobject. ReceiveControl then calls method Jump which has a virtual override in base that gets called. Then JumpController method in the base is called in fixedupdate that does the max in air time check and applies the down force when jump hits apex.