1
\$\begingroup\$

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)Box Colliders

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!

\$\endgroup\$
  • \$\begingroup\$ I notice your player collision box is quite thin. That will make it easy for it to "miss" a collision with the ground if high gravity pulls it more than halfway through the ground's collider. Can you show us any other movement code relating to this character? You may also want to consider adding your extra gravity via a force that acts only on the player in FixedUpdate, rather than changing the whole world's gravity, which can cause unexpected changes in motion elsewhere in your scene. \$\endgroup\$ – DMGregory Jul 12 at 11:45
  • \$\begingroup\$ @DMGregory I just needed to change my Collision Detection to Continuous Dynamic. But thank you for the advice! \$\endgroup\$ – Matheson Jul 12 at 21:54
0
\$\begingroup\$

In your video, I notice that on the RigidBody your Collision Detection is set to discrete. This means there will only be a collision if during a physics frame there is an actual intersection between its collider and another collider.

At the higher speeds you provide, it is quite easy for colliders to miss (or 'step over') each other.

If you set the Collision Detection to Continuous Dynamic, fast moving objects will analyze the whole path their collider takes, and anything that intersects it along it. Mind you, if it intersects with OTHER RigidBodies, it will only work properly if those have their Collision Detection set to (at least) Continuous.

Generally, here's your rule of thumb:

  • Discrete: This isn't going to be moving much, or be hit by anything moving much.
  • Continuous: This won't be moving very fast, but could potentially collide with something that does.
  • Continuous Dynamic: This object will move fast, and needs precise collision detection or risk clipping.
  • Continuous Speculative: This object is kinematic, or you have so many Continuous RigidBodies that it is causing performance problems. It is cheaper, but less precise.
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.