Player is repositioned on a moving platform

Hi Guys,

I have an issue with moving platforms, basically, the player position is reset/updated when the player is on the moving platform. Please see the playground link below.

I basically need the player to stick to the moving platform and still be able to move the player with the keyboard while the platform is moving.

https://www.babylonjs-playground.com/#JMMUI9#15

Thanks :slight_smile:

Adding 4 invisible obstacles around the moving platform?

@VoxelCubes
One possible solution, maybe :slight_smile:

...
const moveZ = switchDirection ? moveSpeed * deltaTime : -moveSpeed * deltaTime;
movePosition += moveZ;

if (onPlatform) {
    player.position.z += moveZ;
}

...

if (player.intersectsMesh(floor)) {
    if (!onPlatform) {
        onPlatform = true;
        offPlatform = false;
        //player.setParent(floor);
    }
} else {
    if (!offPlatform) {
        onPlatform = false;
        offPlatform = true;
        //player.setParent(null);
    }
}
...

One way would be to just transform the player position by the velocity of the platform scaled by the delta time post physics step.

Hi Guys,

Thanks for the response, the code in the playground is the simplified version of my game code so I think the solutions above might not work. What I’m trying to achieve is in this video ( https://www.youtube.com/watch?v=rO19dA2jksk )

I have the code working but the player is repositioned a few seconds after jumping on the platform. Please see the video below.

Looks like it’s moving the player back to origin?

I get back Tuesday late so if you have not figured this out by then I’ll post a code example when I get a chance after some other things I gotta do.

I completely misunderstood your original question. I thought you want to move the box by user on the moving floor and don’t wan’t the box to get out.

My point is still the same. You already uses a physics engine, you can avoid directly modifying the positions of objects like floor. Try to use a force to push the floor left and right. See this PG:

@slin Gravity is needed in my game, so this line would not work for me scene.getPhysicsEngine().setGravity(new BABYLON.Vector3(0, 0, 0));

My character is already using velocity for movement but somehow (Looks like it’s moving the player back to origin) as outlined by @Pryme8

Looks like it’s happening because of the code below.
player.setParent(floor);

Not sure how to resolve this but will keep trying. :slight_smile:

What if you do player.parent = floor instead of setParent?

Anyways I’d think updating the players position per the moving blocks velocity * the engine delta time * 0.001 if it’s grounded to the platform would be better.

I get back in town today, but won’t be able to look till tomorrow

@Pryme8 Using [ player.parent = floor ] yields the same result.

Hi Guys,

Still stuck on this and any help will be appreciated. @Pryme8 I tried your suggestion and still no luck. What could be making the player position reset? (based on the video)

Also, can anyone point me in the direction of a moving platform with a player on it done on BJS if there is any?

@Pryme8 So, I investigated a little bit more and I am not sure where the issue is. I used a cube to test and still got the same result. (see video below)

Regarding your suggestion above ( Anyways I’d think updating the players position per the moving blocks velocity * the engine delta time * 0.001 if it’s grounded to the platform would be better. )

Could you please help me out with a playground code? I need the help. :slight_smile:

Another thing, if I keep running around on the platform, it works. (see video below) Maybe that might help.

Platform test with cube

Running around on platform test

just got back into town, here you go:
image

2 Likes

Actually after reading your requirements and seeing how the PG that I just modified would not meet your needs I went back and made some fixes.

This would be a more “correct” answer

Basically you take the platforms current position and then subtract its last position, then if the player is grounded add that in place to the player position.
image

This should suit all your conditions let me know if there is more I did not think of for you.

Sorry it took so long to get this simple of an answer to you, it was just hard to make and example on my phone.

2 Likes

@Pryme8 No worries, thanks for the prompt response, I will try this out and let you know asap.

1 Like

@Pryme8 Thank you for the assistance, worked like a charm!.:slight_smile: It was the second solution you suggested. Although, I noticed some jumpy behaviour when using [ scene.registerBeforeRender(() ] but works well in [ renderLoop ] function. Not sure why yet but will report here if I locate the issue. See videos below.

Using [ scene.registerBeforeRender(() ]

Using [ renderLoop ] function

Again, many thanks. :slight_smile:

1 Like

No worries glad it helped!

Maybe try onBeforePhysicsObservable