Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question a rigidbody moving along with a moving platform

Discussion in '2D' started by Crembotz, Feb 9, 2022.

  1. Crembotz

    Crembotz

    Joined:
    May 12, 2018
    Posts:
    18
    Hi everyone, I followed this video on how to move a 2D character using forces:

    I've run into difficulties implementing moving platforms, I've tried setting the player's velocity to that of the moving platform but this method has a lot of caveats to it and making the player a child of the moving platform is off the table since the player is a dynamic rigidbody.
    The platform is a kinematic rigidbody with no physics materials attached to it, it's being moved by directly setting its velocity every `FixedUpdate` call. I've also tried moving it using the method `MovePosition` but this results in having the rigidbody's velocity being (0, 0) constantly.
    I'm open to any suggestions you have, thank you in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Then MovePosition and setting its velocity will have no effect and you must set the transform.position.

    Check out the docs... I quote:

    .isKinematic:

    Description
    Controls whether physics affects the rigidbody.

    If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. The rigidbody will be under full control of animation or script control by changing transform.position.
     
  3. meebou

    meebou

    Joined:
    Mar 6, 2017
    Posts:
    40
    If you have a solution, i would be glad if you would share it with me, i have a similar thread here, searching for answers. I have also kind of a moving platform that is kinematic, when the player is on the platform i turn him also kinematic and child him to the platform etc... kind of works, but not really good.

    also, what kurt-dekker posted was from "rigidbody" and not "rigidbody2d" documentation. so is it the same? because this text is not from the rigidbody2d.iskinematic documentation :) and what if you have iskinematic and also "use full kinematic contacts" checked? ....
     
  4. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    Thanks for sharing that video, that's an interesting approach for movement that I'm going to have to play around with. If you've implemented the movement code from the video, I would think you could just add the platforms velocity to the targetSpeed variable.
     
  5. Crembotz

    Crembotz

    Joined:
    May 12, 2018
    Posts:
    18
    That's exactly what I did, but the character slides around while standing still for some reason. It's not the friction that's causing it since I don't apply if no input from the player is recieved
     
    HunterProduction likes this.
  6. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    I wonder if maybe it's due to the acceleration and deceleration in the movement code. If the player is on a platform that's moving at a constant velocity, you wouldn't want to ramp up/down to match that speed, you'd want it to be instantaneous.
     
  7. HunterProduction

    HunterProduction

    Joined:
    Sep 11, 2020
    Posts:
    4
    I am in this exact situation. I'm working on a game where my 2d character (moved with rigidbody and velocity) stays on a boat, which is a kinematic rigidbody. When he drives the boat, it is set to kinematic and fixed in driving position. When he stops driving, I want it to become able to walk inside the boat keeping the same inertial frame of reference of the boat. But as you also said, the character for some reasons "slides" faster or differently from the boat.
    I saw a lot of videos talking about moving platforms, but all of them consider the platform NOT AS RIGIDBODY. I think that's the problem, but I'd like to keep the kinematic rigidbody on my boat, because I don' want to manage velocity and acceleration computing them by hand using transform...
    Any advise about this?