- Home /
Stairs and Step Offset, Character Controller X rigidbody, best solution for character?
Hi guys,
Im new to Unity and trying to build my first prototype game. Its a first-person game.
So Ive encountered my first questions/problems for this development.
Starting with basic mechanics, wich is moving around, I cannot find the best solution for the options I have wich would be to make a character movement/collision using:
1- Character Controller on a capsule. Ive applied this pre-made script to a capsule and created an additional movement script, just to move the capsule around, linked the camera as child of this capsule with the Mouse Look scripts to look around. This seems to move the capsule around and the mouse look are OK, but when I try to move up some stairs (geometry) with 0.2 meters of height (20 cm), the capsule gets stuck. Ive set the Step Offset option from values ranging from 0.1 to 1.0 and still the problem persists (Im using the default capsule size wich is 2m height and followed the instructions etc.)... Also, seems to me that this Character Controller component will not behave as real physics, for example, suppose Im walking and tumble on street cones, or trash cans or any other object that a character would easily move or knock down, I dont think this Character Controller will give me this possibility?
2- Rigidbody on a capsule. Ok, so I tried this other alternative, made the movements by applying AddRelativeForce and the character moves around and the mouse look are OK, gravity works OK. Now, I think this option will let me handle the physics with objects Ive mentioned like street cones, trash cans etc. so this would be OK also. The problem now are: Stairs and inertia of movement. The stairs now work better than Character Controller, but the character tends to slow down when going up the stairs, and the movement is rather irregular, because of the stairs so you can actually "feel" the chacter moving up stairs. The other problem is inertia. Since it is adding force, when turning the character (with mouse look) 90 degrees sideways, the character still moves a bit in the original direction, because of inertia I assume, so this gives a "sliding" effect wich is not good.
So I have thought of creating flat ramps for the stairs but, this will give an overload of work not only for modeling but to set wich objects in game will actually be the colliders, so, for instance in a scene with buildings, houses that use stairs, this would give additional and tedius work of having to create extra geometry just for the ramps, setting these as colliders instead of importing all and setting them as colliders.
So, I hope I have not written too much for you to read, but my question is:
What would be the best solutions for these troubles? 1-(Stairs/Step Offset with Character Controller) 2-(Stairs/Inertia with Rigidbody)
Would someone with experience please enlighten me on these subjects?
Thanks in advance,
Answer by Proclyon · Nov 22, 2010 at 02:57 PM
Problem 1:
Make it look like stairs but just make a sloped area that smoothes over it. Don't make blocks you have to move over, make stuff look like that but in fact you just "slide" the collider over the surface of the area. If you REALLY want realistic movement you need a serious locomotion system (And that is not beginners stuff for sure, and most certainly more work then fixing the level again, just slam invisble rectangles and boxes over the places). Link for a proper and free one here. Yes totally legal. Just don't make money with it and read the documentation and it's all good.
Problem 2:
Using force is literally kicking the character around, u may even get problems with rotations and it's really hard to "throw" yourself as person from one room to another with 100% accuracy now isn't it. Not to mention there are no air brakes for you. So it would look silly aswell. The movement made by the Move function that exists as a property in the Character Controller should be used instead. It already checks for collisions. Just keep track of a movementDirection vector 3 and edit that till the end of the script. Then call the move with the vector with whatever changes needed to be add and ignore using force except for stuff you want to "hit, kick or blow up"
Short version:
Rigidbody + physics + addforce = kicking shooting punching hacking slashing throwing. NOT MOVING!!! (In short, the fun stuff :D)
CharacterController --> Controlling characters, supports just about everything characters need to do already!
Sloped edges = Quick and efficient Locomotion systems = Super realism is possible, hard and not easy to debug. Requires programming skills and time (or community free stuff)
EDIT 11/22/2010 23:02:
For collision methods built into Unity/PhysX look here
There are 6 methods you find interesting.
OnCollision is the regular kind of collision between a monobehaviour collider or rigidbody and returns the event.
For collision trigger methods check here
OnTrigger is an event that is raised when an object with a collider in the scene has the specification IsTrigger. What happens then is that the collision it self is thrown and can be caught as a variable. You can then specify what to do with it. For example in mid air a collision to the chest from some random debris could do nothing (higher x same y value on the transform) while a collision above with the head could cause the character to lose his/her vertical momentum. That's all up to you of course.
If you would for example make a single rectangle and rotate that with collider onto the stairs. So that the character never actually can even touch the stairs. All you need to do is handle one collision method for this block. Or let unity Auto do it for you with the move system. The offset however is sensitive, keep it smooth, but what the player of the game doesn't know can't bother him ^^
So, as in many examples and based on your answer I decided to stick with the Character Collider for the basics of moving the caracter around. Now, the problem with the stairs is interesting, as I have managed to find a conditional solution for it. I have discovered that having stairs with values of height equal or above 0.2 m, (20cm) is problematic, and that means no matter what values I change in the "Step Offset", the character still has difficulties overco$$anonymous$$g the stairs! Thats weird! So, maybe that should be revised by Unity $$anonymous$$m. So stairs should be lower than 0.2m in height.
Now, problem 1 is partially solved, what about the physics interaction with other objects in scene? Im assu$$anonymous$$g Ill have to check collision with one of these objects in scene and apply a force based on the characters speed and direction vector? Because I understand that the Character Collider does not interact physically with objects by default, so Ill have to do that through scripts?
I don't think the $$anonymous$$m made a mistake there to be honest, I have seen it work without problems before. Pre 3.0 Unity aswell. I will edit more links for you where you can find all about collisions. And some information to help screen to info overload that it may have
Yourre probably right Proclyon, maybe some other setup/variable might be preventing the correct climb of stairs. I also dont think they might have made this mistake and have this out for people to use. Anyways, I have found a way to pass this like mentioned, now just need to think about the physics for objects that are rigidbody... Thanks a lot for your kind help! :)
Answer by iuripujol · May 20, 2017 at 09:17 AM
I have managed to solve this issue with a line that solves not only any type of stairs but also keeps the character attached to the ground, do not miss out, send me an email if you are interested to know how I did it.
It only works with rigidbody + capsule collider at the moment.