3D_for_GML:_Jump_&_crouch - hpgDesigns/hpgdesigns-dev.io GitHub Wiki
1.1 Jumping and crouching
This tutorial is about jumping and crouching (in a first person view). If you take a look at the objects needed (in the gm6 file that’s included), you will see only two objects, one of which is a floors object. The floors object (obj_floors) draws a couple of floors: a ground, clouds and a sky. Take a look at its code and you will see that it’s hardly rocket science. The other object is the camera object (obj_camera) that uses all of the scripts except the script ‘scr_floors’. Let’s take a look at the scripts that are worth mentioning. In the script ‘scr_camera’ there’s the following line:
d3d_set_projection(x, y, z+zsize, xt, yt, zt+zsize, 0, 0, 1); //zsize is character's height from z (feet)
The value zsize is the height of the character measured from the feet up (64 in this game). Basically, three scripts are needed to make jumping and crouching work:scr_jump, scr_crouch and scr_uncrouch. The jump script is pretty self-explanatory. The code from script ‘scr_crouch’ looks like this:
//crouch
if z = 0 then {zsize = 32;}
This makes sure that you can only crouch when you are on the ground. The value zsize is set to 32, which is half the figure’s height in this game.
1.2 Uncrouching
The code in ‘scr_uncrouch’ is:
//uncrouch
zsize = 64;
It simply makes sure the character is standing when you release the crouch button.