- Home /
Realistic gravity and physics on a playable character. How?
This has been bothering me for a while now - I have a 3D character, scaled to par with official Unity assets, about twice as tall as a 3D Cube. In order to move this character responsively and for him not to slide like he's using rollerblades, I have "Drag" on his rigidbody set to about 12-13.
But, this makes him actually behave like a feather and any "Drag" value other than 0 makes for severely unrealistic gravity physics, since they don't take into account his mass (which would impact air resistance). You might say "Increase the gravity", but this makes him unable to move at all - until I factor his moving speed by a couple of hundred thousand - in which case the controls are so unresponsive it's unplayable. However, putting Drag at 0 makes it impossible to move the character in any reasonable way using AddForce, as he will accelerate extremely slowly and even still go a lot faster than what he's supposed to once he gets up to speed, then will slide for a while until he eventually stops.
My question is - how are characters supposed to be controlled nicely with realistic physics, without scaling the character to the size of an ant (and thus probably causing its own array of issues)?
I think you can change the mass of the player's rigidbody, does that offer a solution?
Answer by kburkhart84 · Apr 27, 2015 at 03:52 AM
Generally, the character is not actually directly controlled by physics. The problem is that for the most part, game characters don't move all physics based like that. Your character would normally be able to accelerate, stop on a dime, turn around, etc... all that is not something a normal physics setup would be able to handle with ease.
Most games that I've seen that are physics based are not moving the character's themselves with physics, rather via normal translation and rotation, unless the character is actually something that should be physically simulated, like a spherical character or similar. You would still have a rigidbody and collider(probably capsule) on the character, but it would be set to kinematic, which allows you to directly control it, and won't let physics affect it, but would let it affect physics. So a box moving down a hill would not push the player with it, rather the player would affect the box, making it stop or go around, depending on the situation. But, static colliders could indeed not let the player go through, while non-static colliders would be affected, example, the player could push boxes around by simply walking into them.
If you insist that the player be physics based, there are some things you can do. Physics properties can be adjusted though scripting, for example the drag and angular drag can change at any given time, as I believe so can the gravity, and other things. So you could have the drag be lower when the character is falling so that it can fall faster, then turn up higher to control movement speed, and whatever tweaks you need to get it right. But just remember that it may be easier to get the player working better with your own movement code instead of the physics engine.
As an added note: While it is true that any living creature must follow the laws of physics, musculature allows living beings to generate forces themselves.
In general, you would probably want a physics-driven character to have very low drag and very low friction, then modify most their motion yourself (for example, when holding no direction, add force against your current velocity to slow down). Bare $$anonymous$$imum, baseline physics would be intended more for simulation of objects than characters.
I see. Yeah, it makes sense that the player generates local forces that the physics engine can't properly account for. Actually turns out Unity changed the Drag through scripting themselves too, in their Viking village sample pack. They set it to 0 when the character was not grounded. That is probably what I'm gonna do then, because any movement scripting with 0 Drag is going to be painful with its current behavior.
But one of my issues with this is that normal physics objects like items and props would need to have these scripted properties too, unless they happen to be slidy objects. Although I can imagine most of them would feel realistic at 0 drag consistently, or so I hope. Thanks for the answers!
Well, most of your objects aren't going to be characters, so they wouldn't need much scripting of physics properties, as they only react to what the character(and maybe a couple other things) do. A crate wouldn't need to change it's drag at all, whether on the ground or not, once you get the values right in the first place. Only things like characters for the most part would need this kind of thing.