As mentioned in the monthly screenshot thread I have finished my character. I’m just joking with the name. It is surely not the best for your game and has its own flaws but I did some things different than the BetterCharacterControl. I hope you can take the ideas you need and do the best possible control for your game. Enough of the boring introduction. Let’s begin:
Firstly my character contains two parts. It is a capsule standing on a ray. You can imagine the ray as the legs. The ray has only one task. It checks the distance between the capsule and the object under the character. If the distance is lower than the leg height (the ray is a bit longer than the leg) the capsule position will be corrected in order that it has at least the leg height between. The correction is done with setLocation no need to use forces or impulses. What effects do this method have? Firstly you can walk over stairs. The capsule slides over the stair because it is flying. Than the ray corrects the position and you are standing on the next step. As another point you need much higher speeds to fly over hills. The reason behind it is that the capsule doesn’t touch the ground. As a result it won’t get an impulse in the y-direction if it moves uphill. The gravity can push the character down to the surface after it is flying and does not have to work against the impulse. Also surface irregularities are handled smoother. You only get corrected up and then fall down due to gravity but no impulse acts on you which pushes you in the air because the capsule doesn’t touch the surface. Because we don’t touch the surface we can set the friction to zero (It doesn’t matter for the movement). Instead we can’t get stuck on walls if a force pushes the character against it.
That was the biggest trick. The remaining part is simple physics:
I move the character by using horizontal forces. I have an acceleration value. To accelerate the character I multiplay it with the mass and then I have the needed force. Now I can accelerate the character to light speed if I want but I don’t. To get a speed limit you need Newton: We need a slow down force with the same length as the acceleration force. The slow down force should depend on the speed squared. Why? Because you know the behaviour from dropping things in liquids and I copied it. I don’t know if other exponents work as good. The force is
Fs = m*acc*v^2/vmax^2 = m *acc* = Fa
As you see if v is bigger than the max value we will get slowed down because the slow down force is bigger than the acceleration force. Now you can’t reach light speed any more. We only need to stop the character if it isn’t accelerated any more. I use impulses here because I don’t think of a force which drags the character back. For me this slowing down process is more like running and taking a bit speed out from every step. The impulse in x direction is:
-velocity.x*m*0.05f
0.05 is a magic number which produces a nice slow down effect. If you set it to 1 the character will immediately stop.
Jumping is done with impulses too. The rule of energy conservation gives us a term like this:
v = sqrt(2gh)
If you want multi-jumps you can use multiple impulses.
As you see the math behind a character control is not difficult. You can manage to use SI units to control your character and don’t need do depend on magic values.
Last but not least I want to write about collision detection. At the moment when you shoot at the legs you won’t hit the capsule. There are two solutions. Either use mesh accurate collision detection for your model or use a ghost object which has the size of the whole character. Don’t listen to hits of the capsule.
What does this character better then others? It does never use setVelocity. As a result it reacts to forces and impulses. This is the most important point for me.
Maybe it is a bit hard to follow without images. Feel free to ask if some paragraphs are unclear or if you need more details. I’m open for any discussion about disadvantage you can think of. I’m sure it is possible to put this all in a control but I do sadly not need it as a control…