Using c_move will manipulate the player model by displacing him along his own origin rather than the world origin, when using the relative distance vector.
What this means is, if he's facing left, he'll go left.
It is simple.
First a vector and a variable:
#define speedx skill40
#define speedy skill41 //even if you don't use skill41 and skill42
#define speedz skill42 //define them anyways so you don't accidently use them elsewhere
var velocity = 5;
I like to use skills for this vector. And I like to use velocity as a global variable so I can change it at run time to find a speed that suits me.
Now we need an action to put on our model:
#define health skill1
action moveMe()
{
my.health = 100;
while(my.health > 0)
{
wait(1);
}
}
In most games, you can only move when alive, so I've set it up like this.
Now we need him to respond to keyboard inputs. Each key on the keyboard corresponds to a variable defined in the engine. If the key is pressed, the respective variable is 1, other wiseit is zero. And so from this, we get an equation, which does not need if-branches, and thus is shorter code than some other examples I've seen on the web.
The equation is as follows:
my.speedx = velocity * (key_w - key_s);
The way this works (remember velocity is set to 5):
If w is pressed:
my.speedx = 5 * (1 - 0) = 5 * 1 = 5 //forward movement
If s is pressed:
my.speedx = 5 * (0 - 1) = 5 * -1 = -5 //backward movement
If both are pressed:
my.speedx = 5 * (1 - 1) = 5 * 0 = 0 // no movement
If neither are pressed:
my.speedx = 5 * (0 - 0) = 5 * 0 = 0 // no movement
Now we apply the same equation to pan, except with A and D, and then plug the vector into c_move():
action moveMe()
{
my.health = 100;
while(my.health > 0)
{
my.pan = 5 * (key_a - key_d) * time_step;
my.speedx = 5 * (key_w - key_s) * time_step;
c_move(my,my.speedx,nullvector,IGNORE_PASSABLE | GLIDE);
wait(1);
}
}And voila, a very simple movement code, that walks the player in the direction he faces.
Note, I added time_step to the action, so he walks the same speed, regardless of FPS.
since my.speedx is being treated as a vector, my.speedy and my.speedz (skills 41 and 42) are also being modified by c_move, so my.speedy can be used for strafing and my.speedz can be used for jumping/gravity.
Note the two vector parameters in c_move. The first one is relative distance. Think of it as driving a car. This means that while you may turn from going north to going south, you are still going forward relative to the car. Relative uses the model origin.
The absolute distance vector is the opposite. Imagine the car hitting a rock and flipping upside down. Now if the car fell down according to its origin, with c_move, it would fall up. But if you use the absolute distance vector, it falls down, according to the world origin, and not the car's origin.