I’m having trouble making my player move along with the platform he is standing on.
To make the player control correctly, pretty much of of the player’s movement is calculated within the player’s script, which is then applied as a vector to its Rigigbody2D. So there is no force pulling the player down unless my code has deemed the player to be in the air (done by checking for collision at three points beneath the player) and no motion to the left or right unless the player is trying to move (or some other special circumstance.)
So by default, if the player stands on something and that thing moves, it has no effect on the player. The platform can move forward and the player will remain absolutely still until the platform is gone, and then the player falls.
So the first method I tried to fix this was to have the moving platform send its velocity to the player’s script, which then adds that velocity to the player.
if (bGrounded && PlayerOnGround == GroundType.MovingPlatform) {
newVelocity += currentGround.GetComponent<MovingPlatform>().publicVelocity;
}
rigidbody2D.velocity = newVelocity;
It was largely functional, but the player is somewhat delayed from this motion. Overall it’s fine, but when the platform goes down, that delay can cause the player to lag too far behind the platform, and the player script fails to find any ground beneath them, and they go airborne. Also most of the motions I want to use for my platforms don’t work as well as velocities, so I have to convert their motion into velocity to send it to the player, and that data gets much more off because the player is fed inaccurate motion.
So I tried a different method where the platform would calculate its difference in position each frame, then adjust the player’s actual position according to that difference.
if (standee != null) {
Vector3 change = transform.position - lastPostion;
standee.transform.position = standee.transform.position + change;
standee = null;
}
This method kept the player exactly in sync with the moving platform, and I had no problems with moving down or in any direction, and it overall works perfectly. However, I now ran into a problem when the player tries to move while on the moving platform. Suddenly the player walks incredibly slowly. Some effect of having its position changed every frame is causing the rigidbody’s velocity to not have a proper effect on the object.
I’m wondering if anyone has any ideas for something else I could try. I honestly don’t know what else I could do; I tried moving the player’s position and I tried moving the player’s velocity, and that’s pretty much the extent of the ways you can move an object. And if changing an object’s position causes its velocity to change as well, well, I have no earthly idea how to fix that.