Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Falling through floor when resetting collider

Discussion in 'Scripting' started by _gish, Sep 2, 2015.

  1. _gish

    _gish

    Joined:
    Mar 28, 2015
    Posts:
    39
    Hey everyone.

    So, I have successfully implemented a slide animation with my character, but I am running into a problem where the player falls through the floor whenever a slide takes place.

    I have realized that this is because I am re-sizing the players box collider (to make it more narrow, so that the player can slide under things), but when the player stops sliding (aka the slide button is released), the box collider goes back to the normal size, and since the player is in a sliding position, the bounds of the original box collider are beneath the floor, so the player falls through it.

    I have tried messing with different centering and sizes for the box collider, but nothing seems to work.

    Does anyone have any advice for something like this? I would really like to get these slides working.

    Code (CSharp):
    1. if (Input.GetKeyDown (KeyCode.LeftControl))
    2.             {
    3.                 sliding = true;//Set sliding bool in code to true
    4.                 animator.SetBool ("Sliding", true);//Set Sliding bool (in Unity) to true
    5.                 targetSpeed = 0;
    6.  
    7.                 playerPhysics.SetCollider (new Vector3 (0.38f, 0.64f, 1.17f), new Vector3 (-0.05f, .30f, -0.16f));//Set the size of our collider box when the player is sliding (size, center)
    8.             }
     
  2. DrSnake

    DrSnake

    Joined:
    Oct 17, 2014
    Posts:
    33
    The attached code is the code for when sliding starts right?
    What is the code for when sliding stops?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    You could give your character two colliders - one for the upper half, and one for the lower, and then deactivate the one for the upper half when you slide.

    Resizing colliders at runtime while that collider is colliding with things is generally a bad idea. If you insist on doing it, try to ensure that the collider's bottom never changes position.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Simply lift the collider up...
     
  5. _gish

    _gish

    Joined:
    Mar 28, 2015
    Posts:
    39
    Sorry, I should have included that.

    This is what I am doing when sliding stops (when the speed becomes less then a certain amount)
    Code (CSharp):
    1. if (sliding)
    2.             {
    3.                 if (Mathf.Abs (currentSpeed) < .50f)
    4.                 {
    5.                     sliding = false;//Set sliding bool in code to false
    6.                     animator.SetBool ("Sliding", false);//Set Sliding bool (in Unity) to false
    7.  
    8.                     playerPhysics.ResetCollider ();//Calls the reset collider method to set the collision box to the original size
    9.                 }
    10.             }
     
  6. _gish

    _gish

    Joined:
    Mar 28, 2015
    Posts:
    39
    Thank you for the response. That was the first thing I tried, however, it was unsuccessful. Shifting the collider up causes the player to sink partially below the ground level, and then once the collider is reset to the original size, the player falls through.
     
  7. _gish

    _gish

    Joined:
    Mar 28, 2015
    Posts:
    39
    Thank you for the response. How would I go about disabling one of the two colliders if I wanted to try it this way?
     
  8. _gish

    _gish

    Joined:
    Mar 28, 2015
    Posts:
    39
    Update: Hey everyone, thank you all again for the responses. I have got this working now.

    For some reason, using the Box Collider editing tool to get the exact sizes of the "sliding" box collider worked (even though my other dimensions were only thousandths of a unit off from the new ones), and my player is now able to slide under obstacles.

    Thank you all again!