Home > Software design >  Issue with player landing on moving platform - Infinite Runner
Issue with player landing on moving platform - Infinite Runner

Time:10-17

I’m making an infinite runner game and have it set so that the platforms move to the left rather than the player moving forwards. My issue is that I have a platform the loops up and down as it moves left and when my player lands on this specific platform, they start bouncing off the platform, which isn’t what I want.

Moving Platform GIF

One way to solve this is by having the player as a child of the platform, which does work and stops the bouncing, but it also means that the player is then moving left along with the platform.

Moving Platform (Child) GIF

I know why this is happening as a child object will follow the parent object, but I can’t find a way to have my player act to not move with the platform without the bouncing issue.

The player has a rigidbody and box collider attached, the moving player only has a box collider.

Player jumping script:

if (Input.GetMouseButtonDown(0))
{
    player_RB.velocity = new Vector2(player_RB.velocity.x, 20);
}

Moving platform script:

private int moveVertical;

void Start()
{
    moveVertical = Random.Range(1, 3);
}

void Update()
{
    transform.position -= transform.right * (Time.deltaTime * 7);

    if (transform.position.x <= -20)
    {
        Destroy(gameObject);
    }

    if (transform.position.y < -2f)
    {
        moveVertical = 1;
    }
    else if (transform.position.y > 2f)
    {
        moveVertical = 2;
    }

    if (moveVertical == 1)
    {
        transform.position = new Vector2(transform.position.x, transform.position.y   5 * Time.deltaTime);
    }
    else if (moveVertical == 2)
    {
        transform.position = new Vector2(transform.position.x, transform.position.y - 5 * Time.deltaTime);
    }
}

Can anyone offer a solution to this? I just want the player to land on the moving platform normally without bouncing or moving to the left with the platform.

  • UPDATE -

Still having no luck with this, although appreciate the suggestions so far @LewdAngel. I've created and provided a link to a test project, would be grateful if someone can have a look and see if they can sort this issue. Figured it maybe easier to look into this with a project as perhaps the way I've set up the project means the other suggestions wouldn't work.

Test Project

CodePudding user response:

Create a new Physics Material and set its Bounciness to 0, and Bounce Combine to minimum. Then apply the material to your platform prefab or manually to all platforms. It should prevent the bouncy behavior.

As for moving together with a platform, just revert to your previous implementation where Player wasn't a child object.

Physic Material in Unity Docs

  • Related