So I have been stuck on this for the last few days, researched and tried a lot of things but still can't figure this out.
I have a player with a Rigidbody and a moving platform with a Rigidbody as well. The platform is moving back and forth and I want the player to stay on it when he's on.
So when the player jumps on the platform, the platform become it's parent. This is working fine, no problem on that end.
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Moving Platform"))
{
transform.parent = collision.gameObject.transform;
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Moving Platform"))
{
transform.parent = null;
}
}
The thing is, since the player has a Dynamic RigidBody, it looks like the movement of it's parent does not affect him. I can see his position moving in the inspector, but it's not really moving in the game.
When I set the body type to kinematic, the player stays on the platform but then I can't find a way to actually move while on it. So he gets basically stuck on the platform.
Is there an easy fix for this ? Or should I completely change the way I am making these moving platforms.
(BTW, every movements are handled with physics)
When you move the player sometimes it detach from collision even with small margin and will give weird results. try Physics2d.OverlapCircle instead.