- Home /
I needed to change the Collision Detection to "Continuous Dynamic" to fix it.
Problems with gravity and box colliders in Unity 3D
I have a game character that can jump or roll in order to avoid obstacles. He is able to roll while on the ground and he can also roll while he is in the air after jumping. Everything was working fine when I made him roll. I want to make it so that when he rolls while he's in the air after jumping, he would fall at a faster pace than if he was just falling normally. So I did this in my c# script:
IEnumerator IncGravAcc()
{
Physics.gravity = new Vector3(0, -50f, 0);
yield return new WaitForSeconds(2f);
Physics.gravity = new Vector3(0, -9.8f, 0);
}
I call this coroutine in the function that runs when the down arrow is clicked. Everything is working fine. When he rolls while in the air, he falls at a faster than normal rate. However he doesn't land properly and he just falls right through the platform like it's not even there. Here's a video to show what I mean: https://youtu.be/sirc4L3MnqM
I have a box collider, not set to isTrigger, on the player (you can see it, the green box, right underneath him in the video) and the player's also a rigidbody. The platforms don't have rigidbodies on them but they do have box colliders set to trigger. You can see the platform's box collider in the relation to player's box collider in the picture provided (the platform's box collider is the larger one of course)
How do I get it so that the character doesn't fall through? Please let me know if there's anything else you need to know about the project. Thank you for helping me!
Answer by illumin8ed1 · Jul 12, 2020 at 11:12 AM
You said "the platforms don't have rigidbodies on them but they do have box colliders set to trigger". Triggers should only be set when you just want to use the collider to trigger an event programmatically without colliding with anything physically. If the platforms are set to isTrigger=true then they wont stop a player from falling through.
Also, why are you changing the gravity of the whole world and every object just so your character can fall faster? Why not just apply downward force to the rigidbody?
rigidbody.AddForce(-gameObject.transform.up * 10, ForceMode.Acceleration);