Search Unity

Dynamic Character Controller

Discussion in 'Physics' started by BrightBit, May 8, 2015.

  1. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    I would like to program a dynamic character controller for Unity. The most important feature I am aiming at is the ability to climb onto obstacles below a certain step height. It should happen instantaneous and without loss of horizontal velocity. This will be mainly required for a smooth stair climbing experience.

    I've bought both Character Controller packages from the assetstore: UFPS and RFPSP

    RFPSP is a dynamic character controller but has huge flaw in its design and doesn't support stair climbing (at least not the way I want it to work).
    UFPS is much better designed but still has some issues. The main disadvantage for me is that it is a kinematic character controller.

    By the way, I don't want to argue about which type of controller is better. I want the advantages of the physical interactions possible by dynamic character controllers!

    My main question now is how can I achieve this stair climbing mechanism that I want? Here's an animation to illustrate how it should look and feel:



    As you might have noticed, the capsule isn't loosing speed in the x direction while climbing the stairs.

    The BEPU Physics library is free software and contains a dynamic character controller that somehow does what I want. I already tried to investigate it. However, its implementation seems to be based on a cylinder collider, so I don't know how much I will be able to get out of it.

    Do you have any ideas on how to accomplish my goal or do you have any links to papers, other open source libraries etc.?


    Cheers
    BrightBit
     
    Last edited: May 8, 2015
    HHHH_LLLL likes this.
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    I found a suprisingly easy solution for my stair climbing issue. During the FixedUpdate method of my class I am moving the capsule similar to how this class does it: RigidbodyFPSWalker

    Now, my "solution" basically looks like this:

    Code (CSharp):
    1. void OnCollisionStay(Collision col)
    2. {
    3.     float highestStepHeight = -1.0f;
    4.  
    5.     foreach (ContactPoint c in col.contacts)
    6.     {
    7.         float currentStepHeight = c.point.y - capsule.bounds.min.y;
    8.  
    9.         if (currentStepHeight > highestStepHeight)
    10.             highestStepHeight = currentStepHeight;
    11.     }
    12.  
    13.     if (highestStepHeight < maxStepHeight && highestStepHeight > 0)
    14.     {
    15.         Vector3 direction = new Vector3(inputX, 0.0f, inputY);
    16.         direction = transform.TransformDirection(direction);
    17.         // the horizontal velocity in case of no collisions
    18.         Vector3 desiredVelocity = direction * speed;
    19.  
    20.         float d = desiredVelocity.magnitude - rigidbody.velocity.magnitude;
    21.  
    22.         rigidbody.AddForce(Vector3.up * d, ForceMode.VelocityChange);
    23.     }
    24. }
    However, it won't work if the maxStepHeight is higher than the radius of the capsule! But that's absolutely no problem in my case.

    Note: This is a rough implementation! You'll need to check for much more things to make this controller work smoothly, e.g. currently I'm not deflecting the capsule from walls. So it can't climb stairs, if it's also touching a wall.

    Edit: This solution also doesn't really stick to my initial requirement to not loose horizontal velocity but the actual loss is negligible. You won't hardly notice it unless you are really slow.
     
    Last edited: May 9, 2015
    HHHH_LLLL likes this.
  3. HHHH_LLLL

    HHHH_LLLL

    Joined:
    Dec 24, 2016
    Posts:
    2
    Great animation gif! very clearly clarified question and solution, thanks!
     
  4. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    I'll risk sounding dumb and ask the obvious question - have you considered having a ramp collider for your stairs? A lot of big industry titles are doing it behind the scenes exactly so they don't need to deal with the headache you are describing.

    Is there a justifiable reason why you're adding this extra complexity?