How to detect if the player object is on top of a moving platform?

So I am making a 3D game with platforms moving diagonally, horizontally and vertically. Whenever my player collides with a platform and it’s velocity.y is equal to 0 for 2 consecutive frames, the player object becomes a child to the platform so that it moves with the platform but it can also move on the platform.

So far so good, it works perfectly on horizontal platforms and platforms moving upwards. However, if the player object falls down on a platform moving downwards it velocity.y doesn’t reach -0 so it doesn’t stick to the platform and I get this ugly bouncy effect where the cube(player.object) falls to the platform, stops, the platform continues, the cube falls again and so on.

One way to fix this is to remove the condition for velocity.y == 0 for 2 consecutive frames and instead check it in a single frame. But doing it this way will make it possible for the player to stick with the platform even when touching it from below the platform. For example, the platform is coming downwards towards the player, the player jumps and touches the bottom of the platform, its velocity.y is nullified and the player becomes a child of the platform. This is a scenario we want to avoid.

Any ideas how to fix this?

Its possible for you to raycast downwards upon colliding (Checking if it is below you) or by checking by comparing the character Y position and the platform Y position. If it is the moving platform then you could set it to freeze the players Y position to be relative to the position of the platform.

Velocity is still an issue however and you could make it stick to 0 as long as the character is on top of the object through raycast or collision exit checks etc. Your characters jump can remove the constraint on your Y position.