- Home /
How to be tangent with the ground without a character controller?
Hello. New to the Unity engine. Hope this is the right place to post.
I'm making a custom character controller for a game that has dynamic gravity. So I got a capsule mesh and a capsule collider. Good start.
Now the first step is to make the character fall when he's not tangent to the ground, and not fall when he is standing on the ground. I can think of two potential ways to do this. 1) Apply constant downward force and trust Unity's physics to prevent him from falling through the ground. 2) Use raycasting to determine when he comes in contact with the ground and then stop applying a downward movement, in order to keep him floating some minuscule distance above actually touching the ground.
What's the more kosher way to do this? Which is more reliable?
Edit:
Okay. I think I was overthinking this. I didn't realize how much Unity's physics engine gave me.
So the current plan is to have a rotation-constrained rigid body collider for a player character. Gravity is applied (either through Unity's gravity or my own scripted gravity). Then, on every frame, I shoot a real short ray out my feet to determine whether I'm currently grounded.
The physics engine makes me grounded; the ray tells me if I am grounded.
You can read the normal of the terrain below you (By below I mean, pointed to by your player's -Y axis).
Answer by EpiFouloux · Mar 27, 2017 at 03:36 AM
The raycast, in a Coroutine to customize how often the raycast takes place
Why the coroutine? $$anonymous$$y game is essentially a platformer, so it's possible to fall off a ledge at any frame. Wouldn't I then have to do a grounded check on every frame, in the Update() method?
a grounded check would be needed yes. a coroutine is a bit overkill for this.
Because my rep is so low, i have to wait till a mod reviews my post, but there much easier ways to get this sorted...
I don't necessarily need the easiest way, I need the best way. But I'd be glad to hear what you gotta say.
Answer by dvandamme · Mar 28, 2017 at 07:22 PM
playing with forces can get quite messy fast. Id probably start looking at simple collision hulls and using OnColliderEnter() as that will give you control over hull penetration and triggering information. The ground is just a collider after all, and the character is a capsule at this point, so as long as one of them has a rigid body, you'll get collsion info.
Also, you can set up gravity to be any direction - just pass it a vector, so normal gravity is a vector 3 of 0,-9.81,0 and you can set change gravity whenever you like. Obviously, the more things using gravity the greater the potential for unexpected results...