Search Unity

Resolved Dynamic or kinematic rigidbody for 2D platformer character controller

Discussion in 'Physics' started by dev_34Disorder, Feb 14, 2021.

  1. dev_34Disorder

    dev_34Disorder

    Joined:
    Aug 7, 2018
    Posts:
    47
    I'm making a 2D platformer where the player character is mostly moved by code, but also takes some of the rigidbody physics calculations into account. I recently started learning about kinematic rigidbodies and i read somwhere that character controllers should be set to kinematic, since they're controlled mostly by code.
    But that doesn't make sense to me.
    From what i understand, kinematic rigidbodies only collide with dynamic rigidbodies, which doesn't work too well for a player controller. I mean, what about the floor for example? I know that there's ways to work around this, like constantly setting the velocity of every platform to 0, but that doesn't seem like a very elegant solution.
    I've also read that kinematic rigidbodies should be moved using MovePosition as opposed to changing the velocity. But in my game, as i'd guess in many others, the player character is moved with acceleration, not with constant speed. I know that i could handle the acceleration myself, but again, is this all really the right way to do this?
    Either whoever said that player controllers should be kinematic was wrong or my understanding of kinematic rigidbodies is completely off the mark. Maybe both.

    So in the end: 2D platformer character controller
    Should it be dynamic? Are there any drawbacks?
    Should it be kinematic? How do i handle collision and movement?
    Should i completely handle the physics myself? Is it really worth all the trouble writing collision detection, etc.?

    Thanks
     
    MushyAvocado likes this.
  2. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    You can also control a dynamic rigidbody with code, so like you said, that doesn't make any sense. It is understandable why someone would say something like that.

    Kinematic bodies (for the simulation) have an infinite mass, so basically they pass through other colliders + can hit other dynamc rigidbodies (these bodies will be launched no matter what, nothing can beat infinity).


    Kinematic bodies predict their final position normally by using physics queries (raycast, sphereCast, etc) and then moving the body, which in this case it can be done using MovePosition. The reason why MovePosition is used is because you want a smooth transition between the previous position and the next one. This is where interpolation comes in, another important and tricky topic.

    You cannot use velocity as an input for a kinematic body, they don't response to that. It is true that the body might update its velocity value after a MovePosition call (i think 3D kinematic bodies do this, i'm not sure).
    Dynamic bodies can do the same thing, it depends on how you perform that final movement (adding a certain Force, impulse, setting a velocity, teleporting the body, etc).

    You can get almost identical behaviours in both controllers, if you need to handle force, acceleration, velocity or position you can do so without problems with both types (i'm talking from the code pov, kinematic bodies don't response to force, acceleration, velocity, etc).
    The movement logic on top of the controller can be really simple or really complex, it doesn't matter, what matters is that at the end of the day some sort of input value must be send to the character controller. It is very common to send a velocity value, although you can use any input you want. For example, Unity's built-in CC uses a position value via the Move( targetPosition ) API. Your custom controller could take an acceleration value if you want.

    I would say it is not 100% about the type of the rigidbody representing your character, it is more about:
    1. Your movement logic, those components on top of the character controller.
    2. The level of control you want, it doesn't matter if the body is kinematic or dynamic, both can deliver an incredible amount of control.
    3. (after 1. and 2.) the rigidbody type.

    Item 2. is very important. No matter what you do, if you just let things happen (simulation), the result you get is final. This is why people usually have this wrong idea that a dynamic CC is a "floaty" mess moving around, probably because they let the physics to do everything. So, in both cases being in control (recommended for a dynamic CC, mandatory for a kinematic CC) is the best thing you can do.


    Yes, it is very useful to know all this stuff, regardless of the type of controller you decide to make. This will also help you learn how physics + interpolation work in general.
     
    Last edited: Feb 1, 2023
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Just wanted to add that 2D Kinematic bodies can have their velocity set. There's no reason not to. They simply do not respond to forces such as gravity or impulses from collisions.

    Great response btw!
     
    QuinnCG and lightbug14 like this.
  4. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    Oh, i guess i was thinking about 3D. Thanks for the clarification Melvyn!
     
    MelvMay likes this.
  5. dev_34Disorder

    dev_34Disorder

    Joined:
    Aug 7, 2018
    Posts:
    47
    Woooow, this is extremely informative and answers pretty much every single question i posed. Absolutely incredible.
    So far I've been using a dynamic RB for the player and it's working perfectly fine, so i got worried when i read that it should be kinematic. But as you've said, it doesn't matter which one i use, as long as i have the level of control that i need, and it makes sense 100%!
    As far as writing my own physics goes, i've already spent waaay too much time coding for the game that i'm working on right now, so i'll have to try this on some other project in the future.
    Again, thank you so so much for the reply! It connected all the puzzle pieces floating around in my head. You should try making some tutorials in you spare time, you seem to be really good at explaining things :D
     
    lightbug14 likes this.