Just a WIP of a side scroller made with Unity.
The technique used to jump on a platform and stick to it is to parent the player to the platform! Works very well!
Otherwise the player will just slide off the platform when it's moving sideways and when it's going up, the player will tremble.
The platform has a rigidbody2d set as fixed and kinematic! Then has a box2d collider attached to it. It just moves following a predefined path.
Platform touch behavior:
/*Player C# scrip */
// When the player touches anything
void OnCollisionEnter2D(Collision2D other)
{
// Remove the parenting of the player
gameObject.transform.parent = null;
// If we touched a platform
if (other.gameObject.tag == "Platform")
{
// Make the player child of the platform
gameObject.transform.parent = other.gameObject.transform;
}
}
// When we exit the collision
void OnCollisionExit2D(Collision2D other)
{
// If we was on a platform
if (other.gameObject.tag == "Platform")
{
// Remove the parenting
gameObject.transform.parent = null;
}
}