Moving Platform

Hi, I created a moving platform that moves vertically. I coded it so that when the player’s rigidbody and platform’s rigidbody comes into contact with one another, the player will become a child of the platform. And once the player is no longer in contact with the platform (onCollisionEnd) the player should return to the root. The issue now is when the player gets off the platform, the on collision end event is not detected. (Causing the player to move vertically with the platform despite the player not being on the platform anymore) Anyone have any idea how to fix this?

VerticalPlatformMovement.prototype.onContact = function (result) {
    // Check if both entities have rigid bodies and the collision involves the player
    if (result.other.tags.has('player') && result.other.rigidbody && this.entity.rigidbody) {
        // Make the player a child of the platform
        this.player.reparent(this.entity);
        this.playerIsColliding = true;
        this.playerEntity = result.other.entity;
    }
};

// Function to handle collision end events
VerticalPlatformMovement.prototype.onCollisionEnd = function (result) {
    // Check if the collision is with the player and they were colliding
    if (result.other.tags.has('player') && this.playerIsColliding) {
        // Revert the player's parent to the root
        this.player.reparent(null);
        this.playerIsColliding = false;
        this.playerEntity = null;
    }
};

Hi @jerry_lee!

You use a dynamic rigidbody for the player entity that is not moving with the platform entity. Only the visual entity is moving with the platform entity. Because of that, the rigidbody is not on the same position as the visual entity anymore.

You could make the rigidbody kinematic when you reparent the entity, but that means you cannot move the player with forces as long as the rigidbody is kinematic.

1 Like

Thanks for clarifying the issue here @Albertos :slight_smile: ! So reparenting the player to move with the platform doesn’t work with a dynamic rigidbody. Changing the rigidbody to kinematic wouldn’t help in my case either since I would require the player to move while they are on the platform.

Would appreciate it if anyone is able to give me some pointers on how to create a moving platform that fulfil my condition. If I just have a script that moves the platform’s rigidbody up and down without any condition, my player sometimes phase through the platform or doesn’t even follow the platform at all.

1 Like

You can change to kinematic body and still have control over the player, as @Albertos suggested. I did something similar here:

https://playcanvas.com/project/1139572/overview/platformer-starter-kit

2 Likes

Aite, will look into it. Thanks! :slight_smile:

Or you can leave the player physical and add foces to move in the same direction as the platform while he is on the platform, and there, depending on the goal, work out different options: whether you need to preserve inertia when jumping, etc.

I think you could use the tween.js by playcanvas to smoothly move an entity with a collision, I’m not sure for how to use kinematic though, as in my experience dynamic and kinematic don’t play too well together. (as in if I tried this the player would get blasted into outer space from standing on the moving platform)