Search Unity

Capsule collider + rigidbody goes through colliders when walking under ramps etc.

Discussion in 'Physics' started by eses, Aug 11, 2015.

  1. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Like the topic says - I've been testing a mecanim setup for 3rd person game and noticed this. I have a character with capsule collider, rigidbody, and a script that modifies position of object and when I walk under a ramp, it's possible to walk under it and character won't stop moving but walks through the ramp... (instead of hitting his head to bottom ramp). Character can walk on top of the ramp without any problems.

    I'm moving character with transform.Translate, might this be the reason? I just find that rigidbody addforce didn't quite work like I wanted.

    Any ideas?

    thanks in advance!
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Last edited: Aug 15, 2015
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    HiddenMonk: Thanks, I'll check the link!

    Overall, I think I'd like to avoid using rigidbodies (but, apparently - I can't). I'm quite new to this stuff.

    Using addForce causes all kinds of "physics object" effects which I'd like to avoid (= to keep things simple so that I can manage it...). Seem like I'll have counter many effects caused by physics system.

    When I make my character run, it becomes clear that turns and sudden changes in speed are not that easy to handle. Or how to do it is not obvious. Character feels slippery. I'm thinking about using drag.

    Using addForce also makes contacts with walls harder to handle - If I make character run diagonally against a wall, if it's touching wall it will turn like a byou towards wall which looks bad. I don't think I had this with transform.Translate. Maybe modifying friction helps.

    Also, using rb.MovePosition, does not seem to be working any better than transform.Translate, which is bit strange. It has exactly the same problem. Maybe I just have to stick with addForce or manipulating rigidbody velocity?
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Have you looked into a character controller? Its seems like its pretty much what you are looking for. Something that collides, but isnt a physics object so it wont push things.
    The big downside is that it can only rotate left and right.

    Worst comes to worst you may need to make your own character controller type thing with raycasts, spherecasts, capsulecasts, etc... but this is a road I went down and still in the end, ended up just going with addforces.

    What physicsy things do you not like?
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    HiddenMonk: "own character controller" -> I think I'm going to that direction. But I try to keep things simple as I'd just like to first learn how things work.

    I had an impression that Character Controller is bit out dated and not to be developed further and that's why I haven't tried to use it.

    I think I can manage wall collisions by raycasting and see if I allow running forward or not and so on.
     
  6. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    As far as I know, theyve added to it in recent updates. I dont think its outdated.

    Maybe this can work for you.
    https://roystanross.wordpress.com/downloads/
     
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    HiddenMonk: Thanks, I think I've seen that one - seemed bit complicated for me :/

    I think I made some progress today - managed to get rid of rigidbody like stuff to some degree using following tricks:
    - When close to walls, don't allow adding velocity to rigidbody if player tries to move forward.
    - Removed friction from wall materials and character
    - Simulate friction using drag when grounded, and stopping
    - Each frame, remove angular velocity from rigidbody - it seem to create nasty spinning movement if character faces wall and movement force is applied to it.

    Not sure if these are too hacky methods, but so far for my simple test today these seem to be working.
     
  8. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Have you tried constraining your rigidbody on the x and z axis in the inspector and just rotating your rigidbody left and right with regular rotation?

    You can also constrain your rigidbody positions, which would basically make it a kinematic rigidbody, except you get information like oncollisionxxx
     
  9. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    HiddenMonk:

    "Have you tried constraining your rigidbody on the x and z". I've already constrained (for time being) xz rotation, and now the y axis. Actually I've done that in another test but not in this case - it works well - seems like it has the same effect as using rigidbody.angularVelocity = Vector3.zero.

    Maybe constraining rigidbody in all axis works even better as I run into couple of issues where I landed capsule collider on a corner of a cube, character started to spin wildly. Anyway it might have been one or other thing as I'm just learning how to setup this kind of character.

    "constrain your rigidbody positions" - I think I'll try this too. Thanks again!
     
  10. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Ah yes, constraining all x y z rotation is what I meant. Its what I have done with my rigidbody character (except I am still using addforces).

    As for contstraining the rigidbody position and moving it, as of unity 5 it seems you cant use moveposition. Not sure what other changes there could have been so this may no longer be of use to you.
     
  11. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    HiddenMonk:
    "as of unity 5 it seems you cant use moveposition" - Yes, I didn't quite register everything you said (it was too late). Rigidbody.MovePosition seems to be behaving very similarly to transform.Translate (goes through bottom of ramps).

    But anyway, I got everything moving using rigidbody commands, basic movement stuff seems to be working OK now (walk, run, turn, stop, climb, fall). Thanks again!

    A note - seems like interaction between character and camera can be troublesome it seems, there is a lot more I have to learn about physics updates.
    Seems like all the movement in character has to happen using rigidbody (which is now the case). Also, some rigidbody forcemodes seem to cause jitter in camera movement.
     
  12. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I currently have my camera parented under my rigidbody character and I set the rigidbody to use interpolation. I also have my fixed timestep at .01 instead of the default .02 but you may not need to do that. Interpolation might be all you need.
    Keep in mind that interpolation will delay your movements. This is a reason to increase your timestep, not for more accurate collision, but for faster reactions.

    You may still need to do some extra collision checks yourself since the physics is not perfect. My character does a dash, and sometimes it would interpenetrate the wall for a frame when dashing into it. I had to do some extra checks during update to make sure I wasnt in walls.

    I have continuous dynamic collision detection enabled, and in edit - project setting - physics my default min penetration set to .01 i think (something lower than the default).

    Having a rigidbody character can be difficult. You need to take everything in account.
    How many colliders are controlled by your rigidbody, since adding more colliders will automatically change the center of mass which can change your rigidbodies behavior (I think you can change the center of mass in script too though). I think if you were to parent a gun with a collider under your rigidbody character, it would change thing.

    Your rigidbodys physics material can be important to. I currently have a physics material with all 0 out and combine friction/bounce set to average on my character. By default, if a collider has no physics material, it will be given a friction of .6 or something (I think you can change the default in the edit - project setting - physics manager)
    I then also have my wall have 0 friction, since otherwise I would get stuck on it.

    Then theres input, since your rigidbody has to be controlled in fixedupdate, and that update will usually run more than fixedupdate, if we did input checks in fixedupdate we would miss some. So you will either need to cache input in update and then check it all in fixedupdate, or do the check in update and then set a bool to true allowing you to do your action in fixedupdate. If you were to cache the input, you may run into other problems in regards to multiple fixedupdates running per frame.

    Then you may run run into problems with how your rigidbody movement script works. For example, in that rigidbodyfpswalker script link I posted in a past post, there is an issue. The script is doing targetVelocity - velocity, and then clamping it. That is needed to have snappy movements, but it comes with a drawback. If there is an explosion and it pushes you waaay far, with that current code, if you were to start walking in the direction you were pushed (or any direction really) you would start to slow down! If the clamp wasnt there, you would slow down instantly to the speed of your walkspeed.
    I currently still just clamp my velocity since I need the snappy movement and cant think of a way to limit my movement properly. You can see me and another guy try to do so here but fail http://forum.unity3d.com/threads/character-controller-versus-vehicle.338240/#post-2189945

    Then theres the little gotchas like how setting a maxangularvelocity might limit your velocity, but even if limiting it, if you add to much angular velocity your collision detection will be bad (or maybe I did something wrong when testing this).

    If you have continuous collision detection enabled, its not actually enabled until a certain velocity is met. This is done for performance reasons on unity side. So, at slower velocity you will still have somewhat poor collision detection.

    Parenting rigidbodies under rigidbodies can cause weird results and I think non uniformly scaling them can also cause issues (didnt test, but people say its bad). I think if you were to scale it weird and did a raycast, the hitpoint might turn out to be in a weird spot.

    Delays with fixedupdate and what not can be troublesome too. I think OnCollisionxxx and OnTriggerxxx run a fixed update frame after the initial collision. I think having interpolation enabled can further the delay. Also, I think when I tested, with interpolaion enabled, when you addforce, the rigidbody will still move that same frame you added force (though it will move at the start slower than without interpolation), however, it will take one extra frame to stop the rigidbody with interpolation set.
    All these delays...fixed timestep, interpolation, etc... can make your character feel less responsive. The problem with increasing the fixedtimestep is that will affect all rigidbodys and waste cpu power on all rigidbodies even though you only cared about your character.
    For my movement timing, I also use a custom time class that had a fixedFrameCount which keeps track on how many fixed frames passed, and I use that instead of regular time to try and get consistent movements. Also, Time.time seems to update every fixedupdate, which I didnt know until recently.

    All in all, the physics wants to be realistic, but we want our characters to be magical. Its a constant struggle it seems =), but youll learn to work with what ya got.
     
    Last edited: Aug 15, 2015
    Phoera likes this.
  13. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    HiddenMonk: Thanks for extensive write-up!

    I hear you - seems like Rigidbodies are not the best thing for snappy and possibly unrealistic movement, but I think it's good place to start learning at least, as I get many things "free" although many there are many things one has to struggle against like you said! I think I'll consider rigidbody+capsule setup a 3D alternative to 2D sprite setup in Unity, I won't expect too much from it :) Bit off topic - IHMO the laboratory demo for example gives very bad impression on how Unity can be used, stiff and unresponsive controls, jerky movement and so on. That's why I was a bit worried about getting anything usable out of Unity 3D character setup with limited coding.

    I do have quite similar setup with materials, friction and Input I think!

    It seems like I got all the basic things working - movement, falling, jumping on top of rigidbodies (movable boxes), using/walking on moving platforms, 3rd person follow camera that moves in relatively smooth manner.

    Some issues still:
    1. Falling and landing on edges. Sometimes test character that uses capsule collider of 0.3m radius will get stuck to edges. It happens occasionally, not all the time. Seems like tuning Physics settings might help, not sure yet.

    2. Moving on moving platforms doesn't seem natural, I can add the velocity of platform to character, but seems like movement doesn't quite match, seems like platform pushes character a bit too much. Backwards movement is slower than it should etc. Don't remember what forcemode I used.

    Not sure if I need these things to work, at this point I just want to create a testing sandbox :) - It's very basic setup but at least I can start testing different things.

    Thanks again!
     
    HiddenMonk likes this.