Search Unity

How to handle falling damage when "riding" an object down

Discussion in 'Physics' started by dgoyette, Jan 14, 2019.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    In my game, fall damage is handled fairly simply: My player controller handles OnCollisionEnter, and checks whether `collision.impulse' is large enough to cause damage or death. This generally works fine, but there's one edge case that's causing me trouble.

    Let's say the player steps onto a platform, and the platform breaks. The platform and the player now start falling down. I'm finding that because the player and the platform fall at exactly the same speed, they never separate during the fall. Even if the fall is very long, eventually the platform will hit the ground, and the player will come to a stop, never firing another OnCollisionEnter. The player effectively "rides" the platform safely to the ground, and doesn't take any damage. This is a problem. It means that the player can survive any fall is they get some other falling object under them.

    The only approach that seems to help is to start handling OnCollisionStay in addition to OnCollisionEnter. If I do that, then on the "landing", the collision in OnCollisionStay has a fairly high magnitude. However, it's generally only half of what the magnitude would be if the player had fallen onto the ground independently. So, the player can still survive what should be fatal falls in many cases. Also, performance wise, handling OnCollisionStay seems a bit expensive.

    So I'm wondering if there are other general approaches to avoid the lower object protecting the upper object from collision when they land together?
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I had similar troubles too, what I did i keep track of the player's velocity and look out for sudden changes, like if you see it go from Vector3(3, -9001, 7) to Vector3(5, 1337, 2) in a frame, you can tell you've run into the situation you described.
     
  3. I would try to do this way:
    - when the platform breaks you have an onCollisionEnter in progress, register that the user is there
    - in theory until you get an OnCollisionExit on the platform the player and the falling platform are riding together
    - if you register this (the lack of exit), then you can use the falling platform's OnCollisionEnter as a proxy and besides handle the damage to it or whatever you do with it, you can notify the player about hitting a surface

    It's not absolutely clear separation of concerns, but it might work.
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    You might move the platform to an other physics layer, one that the player won't collide with.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Some cool ideas in this thread so far. Thanks for the leads.
    • @SparrowsNest Keeping track of rapid changes to player velocity is an interesting idea. I'd need to either only use that approach for collision damage (and do away with onCollisionEnter entirely) to avoid double-counting collisions, but I'll look into that.
    • @Lurking-Ninja That's also pretty interesting, and I think that would layer nicely on top of my existing OnCollisionEnter approach. I could see registering listeners when my player OnCollisionEnters a collider, deregistering on OnCollisionExit, and accepting surrogate collisions for the duration. I wonder if this would still work if the player was riding a "stack" of objects, though. Maybe in that case, only the bottom object in the stack would have OnCollisionEnter called on it, as all the other falling objects in the stack are touching the whole time down.
    • @PGJ That was something I tried briefly, and it obviously does ensure that the platform can't protect the player when falling. But it introduces other weird messy stuff. One specific issue is that if the platform hits something else on the way down, causing the platform to slow its decent, the player just keeps moving through the platform. So I ruled the layer approach out due to how many use cases there are where I need collisions to keep working properly between the player and the platform.
    Thanks again for the ideas.
     
    Lurking-Ninja likes this.