Search Unity

rigidbody character - how to move properly?

Discussion in 'Physics' started by zeropointblack, Jan 18, 2021.

  1. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197
    hi, backstory: so ive been fumbling around with trying to get a decent rigid body character moving around in 3d for months on end still with pretty awful results. (unity gravity is worthless, and is unrealistic, so i am going to have to code my own gravity, sure why not, but until then)

    is there a way to get a rigid body character to MOVE realistically without...


    1. bouncing everywhere? (already have material 0 bounce etc etc.) its math, i know. speed plus angle up on terrain = you go up. i get that, but it sucks.

    2. i try to fix that by increasing DRAG, but then my player cant walk up a simple incline. so now what?


    i know i cant be the only one who has run into these problems. i will continue to waste countless months trying to figure out a solution, but can someone point me in the right direction first? thanks.

    p.s. should i be switching BACK to my convoluted character controller? rigid body seemed like a better switch, but i guess not. what am i missing.
     
  2. Polyfemos

    Polyfemos

    Joined:
    Nov 13, 2020
    Posts:
    25
    Have you tried adding the builtin CharacterController component instead of the Rigigbody+Collider components?
     
  3. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197

    of course i have. as i said, i originally used one before, but it was too convoluted. and for this project i want lots of physics going on.

    the good thing about it was, i was able to run very fast over changing landscape without popping 50 feet into the air and floating down slower than a friggin feather. so THAT was good, but everything else is missing. i may have to go back to that, but then why code my own physics and gravity if rigidbody has one, right? i dunno.
     
  4. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    Well, first of all, a character controller (physics controller if you want, no logic involved) will not perform "well" if the only thing it does is applying some F force towards a D direction based on you I input. You'll need to measure the environment, do some basic (or not, depending on the complexity you want to achieve) physics queries in order to check the ground, moving platforms, obstacles, etc. All these stuff will allow you to make a more informed decision when tweaking/modifying the RB properties (mass, drag, velocity, etc).

    The movement you are getting now (using pure physics + capsule) is very realistic, and that's the problem, you don't want realistic movement in this scenario. The most obvious reason for this is that humans are not capsules, hacks will become necessary at some point, whether you like it or not. Some like to tweak drag/mass/gravity, etc, based on the current situation (the "Physics side" of things), others (like me) like to predict everything (in the same way a kinematic controller works, the "Kinematic side" of things) before changing force, velocity, etc.

    Use the ground information and project your velocity using the ground normal. I wouldn't depend on drag to do these things, maybe you could combine drag with velocity projection, especially if you want to control acceleration/deceleration and max speed.

    If you want to reduce that bouncy effect maybe you can use the "LateFixedUpdate" coroutine (not really its name) to get the distance to the ground. After that, just add that velocity to the character (distanceToGround / dt ).

    Code (CSharp):
    1. IEnumerator LateFixedUpdate()
    2. {
    3.     YieldInstruction waitForFixedUpdate = new  WaitForFixedUpdate();
    4.     while( true )
    5.     {
    6.         yield return waitForFixedUpdate;
    7.  
    8.                 // Check the distance to Ground
    9.                 float distanceToGround = GetDistanceToGround(); //<-- To implement
    10.                                
    11.                 // Add velocity to the character
    12.                 Vector3 snapToGroundVelocity = distanceToGround / dt;
    13.                 rb.velocity += snapToGroundVelocity;
    14.          }
    15. }
    It is not a perfect "snap to ground" solution, but at least it will help.
     
    Last edited: Jan 22, 2021
    zeropointblack likes this.
  5. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    @lightbug14 is correct that it's not a matter of physics being unrealistic, but rather too realistic for your expectations, and possibly too complicated to intuitively setup for your needs.

    It seems like physics is more in your way than helping you if you need to worry about all this, but if you know you need it, have a look at another user going down that rabbit hole in this thread https://forum.unity.com/threads/ground-detection-for-sharp-angled-slopes.1037224/

    WIP implementation aside, the general algorithm demonstrated in that thread is sound and similar to how it was done in Quake 3:
    • Determine ground
    • Project desired movement on the ground plane
    • Calculate acceleration that will get you from your current position towards the projected point.
    That way if the plane is sloped, movement will stick to that slope and there is no bounce because your calculated movement is along the ground instead of continually slapping against it. It's also written in a way that you can easily take their solution and slowly replace parts of it with your own code as you need.
     
    lightbug14 and zeropointblack like this.
  6. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197
    im going to try this, thank you.

    and i saw your character controller vid, the other day when you posted this. simply amazing. you are a god of movement programming. simply incredible, and inspirational.




    sounds great. im going to give this general idea a go, together.

    whats really surprising and unexpected is that all of this stuff is needed. when we are all starting out, we never expect stuff like this to happen until we get there.
     
    lightbug14 likes this.
  7. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    447
    Thanks a lot for the nice words! Hahah i'm not a god, i'm just a guy doing Lerp and MoveTowards :D.

    Yeah, this is one of those things no one tells you at the beginning, simply because most engines (Unity, Unreal, Godot, etc) include a CC by default, they obviously expect you to pick the built-in CC (which is usually good enough) and not ask many questions. So, there is no need for Unity to teach you how to make one (one less thing to worry about, which is good for you and for them).
    I remember years ago, when i switched from my crappy RB to the built-in CC, what a joy! every single issue related with the old CC was gone!... well ... at least until i needed to rotate the character my character in order to adapt to a different gravity direction :(.
     
    Last edited: Jan 22, 2021