- Home /
Unity rb Player not sticking to a moving platform
I'm working on 2D Unity game. I want to make a moving platform on which player can stand, move, jump.
Player is dynamic Rigidbody2D with this settings
Platform is kinematic Rigidbody2D with this settings
For moving player I'm using velocity
private void FixedUpdate()
{
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
For moving platform I'm using MovePosition
private void FixedUpdate()
{
rb.MovePosition(transform.position + transform.right * Time.deltaTime);
}
Player doesn't stick on platform, when platform gone player will fail down (because of gravity)
The Player must be dynamic Rigidbody2D, the platform does not have to be kinematic Rigidbody2D.
But when I remove physics from platform (also set player to be child of platform) player moves slower on the platform than on the ground and there is jittering that's why I made platform kinematic rb
Can someone tell me why player not moves with platform when both uses physics? And how to fix this
Thanks
Answer by RealFolk · Feb 05, 2021 at 12:55 AM
The player needs to be parented to the platform onCollisionEnter and unparented onCollisionExit. Respond if you need more help than that. This script goes on the player.
private void OnCollisionEnter2D(Collision2D hitObject)
{
if (hitObject.gameObject.tag == ("FallingPlatforms"))
{
transform.parent = hitObject.transform;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == ("FallingPlatforms"))
{
transform.parent = null;
}
}
Player rigidbody is dynamic. Moving platform is kinematic. Both SleepingMode is start Awake and CollisionDetection is Continuous. I just tried this and it works perfectly. @kole_dole
When I use parenting player doesn't move as expected ( is slower and there is jittering in movement) because of dynamic Rigidbody2D of the player. When Player is dynamic, Platform static, movement is as I expect, but player doesn't move together with platform (and then parenting doesn't work). Please help
I move the platform with this:
transform.position = new Vector2(transform.position.x + moveSpeed * Time.deltaTime, transform.position.y);
I move the player with this:
public int playerSpeed = 10;
private bool facingRight = true;
public float moveX;
public float moveY;
private Vector3 moveDirection;
// Update is called once per frame
void Update()
{
moveDirection = new Vector3(moveX, moveY).normalized;
}
void FixedUpdate()
{
Player$$anonymous$$ove();// calculates left/right movement at a fixed rate
}
void Player$$anonymous$$ove()// horizontal movement
{
moveX = Input.GetAxis("Horizontal");// sets moveX to left thumbstick
moveY = 0;// 0 because we dont move up and down
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
}
Answer by Austin_A · Feb 05, 2021 at 07:01 AM
If parenting does not work try calculating the offset so in fixedupdate make a vector2 offset = new vector2( player,transform.position.x- platform.transform.position.x,0) That will give you the offset to do rb.MovePosition((player.transform.position + offset)*time.fixeddeltatime). I’m assuming the rigidbody is the players and that this script would be attatched to the play. On collision enter you would set a bool isOnPlatform to true and on collision exit set it false then only run the above code while isOnPlatform is true. @kole_dole
Sorry it should be platform.transform.position + offset in the moveposition params not player.transform.position
I'm using $$anonymous$$ovePosition for moving platform, rb.velocity for the Player. As I understand you sugget me to change position to player using $$anonymous$$ovePosition not rb.velocity right? Thanks
Yes use move position for both. This way it will move the platform and the player at the same time in fixed update the same distance
Your answer

Follow this Question
Related Questions
Is updating from Gamemaker apk to Unity apk possible in the Play Store? 0 Answers
blend4web is better than unity WebGl ? 1 Answer
Unity 5 (All turns White) 0 Answers
How to disable close button of Editor Window? 0 Answers
GameObject.Find Unity 5.3+ 1 Answer