- Home /
Moving platform grab the player
Many threads concerning moving platform with this kind of code :
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "MovingPlatform")
{
transform.parent = other.transform;
onPlatform = true;
}
}
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.tag == "MovingPlatform")
{
transform.parent = null;
onPlatform = false;
}
}
the code works very well, but if the player has the same "height" , when the platform moves right, the player is attached to the platform, and stick to it. platforms push and pull the player. Push is cool, but not pull :/
Check the direction of the player in relation to the object it's colliding with. Direction would always have to be Up right, otherwise the player is not on top of the object. So just check that in the collision enter.
Answer by Pachanka · Mar 16, 2021 at 10:28 AM
Finally with a detection of the position (y axis) of the player :
if (other.gameObject.tag == "MovingPlatform" && transform.position.y > other.gameObject.transform.position.y) { transform.parent = other.transform; onPlatform = true; }