Movement Types in Game MakerA Game Maker Tutorial by Hero of Hyla
We're going to be discussing different types of movement which are possible in Game Maker. Different games, of course, need different ways to manipulate the characters. In this tutorial, we will cover:
-Directional Manipulation (I can't think of a good name for this)
-Standard Pixel Based Movement
-Tile Based Movement
-Acceleration To a Max Speed
-FPS Style Movement
-Space Movement
-Car Movement
We will look at the pros and cons of each system, how to implement them, and what they're good for
But first, let's set up a standard character that we'll use for all of these tests. I've gone ahead and made a 15x15 circle character that has a radius on the right side. Here's our guy:

I've chosen 15x15 for his size, instead of 16x16, because I wanted a clearly defined center point. This will be important for the movement types involving rotation. Set the origin of the sprite to the center (7,7 on this sprite). Go ahead and make an object for this sprite. I've called mine objChara (I use camelNotation).
We're also going to make blocks that the player can't move through. I'm just doing a 16x16 black square. Make it solid and
visible in the object settings.
Directional ManipulationBasically, this is a style of movement where the character is moving a set speed, but you can choose a direction of movement via the arrow keys/input device. This movement type is good for games like Snake or Pacman.
This is probably the easiest type of movement to implement. All you need is key pressed triggers and "Start Moving in a Direction" commands. Also, if you want to start with the character already moving, you should make a "Create" trigger and set the move speed. Here's how the code looks:


Go ahead and test it out - first make a room and put your player object in it.
Well, it works so far. But how about making the player bounce off of walls? This is VERY easy to accomplish. Just add a "Collision with objBlock" trigger. And tell it to reverse horizontal and vertical direction.

Now lay some blocks around the room, and BAM! You've got something awesome.
So, to sum it up:
Pros: VERY easy to implement
Cons: Not much versatility for the player
Standard Pixel Based MovementThis is a very versatile movement type. It's great for sidescrollers, top down views, etc. The one problem is that it doesn't record the speed when the character moves, so friction is difficult to work with.
Let's get started, shall we? Clear the events for the character object, or make a new object. Make triggers for the arrow keys (not key pressed, just keyboard). Then, in the actions, make a "jump to position." Check the "relative" box. Choose an amount for speed, I'm choosing 4. So, for left, you'd do x:-4, for right you'd do x:+4, for up y:-4 for down y:+4.

Repeat for each direction
Now, try it out. It should move around, and diagonals work too. But, there's one problem: you move right over blocks. Well, setting the speed to 0 on collision with blocks comes to mind as a solution, but remember that I said that the speed is ignored in this movement type. So what you need to do is make a check if a position is collision free. Since my move rate is 4/second, I'm going to do a check like this: If a position 4 pixels in front of me is clear, move 4 pixels. Here's the example for moving up:

That should work! Try it out. The only problem I can see is that diagonal movement is a bit fast, but that's a limitation that we'll have to deal with.
To sum it up:
Pros: Versatile, easy
Cons: Doesn't use Speed or Direction
Tile Based MovementIf you've used rpg maker, or played the nes/snes Final Fantasy games, you know what this is. Movement takes place on an invisible grid.
For this one, I won't be using our circular hero. Instead, I'll be using an RPG Maker 2k/3 sprite. Now, importing the sprite could be a bit of a problem. How do you get a character sheet onto a program like GM? Easy! Go to the sprite editor, and click on Edit Sprite. Go to file>create from strip, and select the file you want. Adjust settings to get the right sizes

You'll have to import each facing seperately. Add another copy of the standing still sprite at the biginning of the animation. In each image, set the bounding box to the bottom center 16x16 pixels.
Now, we're going to work on downward movement first. The first thing you want to do is make the system check whether the image is aligned to the grid. Now, Tell it to start moving down (I used a speed of 2). Then set the subimage to -1 and the speed to .25, if you're using my move speed. Repeat these with a different direction in the move action for each arrow key.

Now he's moving, but wait! He's not stopping!
Now do a <no key> check. Check if the image is aligned to the grid, then make a move action with the square in the middle checked. Set the variable image_speed to 0.

That should do it! You should be able to figure out the collisions for this one by now, so I won't cover them.
To sum it up:
Pros: Easier to manage movement, in terms of planning out layouts. You don't have to worry as much about precise collision checking, because everything is assumed to have a 16x16 collision box.
Cons: Feels restrictive for the player.
Acceleration to a Max SpeedThis isn't a movement type on it's own, but it's an important thing to understand for the next few movvement types. You'll be using a bit of coding here, but it's very simple.
We'll just be focusing on the "Keyboard Up" trigger and the "create" trigger here. Now, in the actions, go to the control tab and execute code. Put this in the input field:
CODE
{
if (speed < 4) speed = min(4, speed + .3)
}
Basically, this checks if the player's speed is less than 4. If it is, it either sets it to 4, or increases it by .3, whichever is lower.
Now, go to the "Create" trigger and set the friction. It MUST be less than the rate of acceleration (.3 in this case) or the character will not be able to move. With friction, the character will slow to a stop on its own after the movement key is released.
You can repeat the acceleration code on the keyboard:down trigger too, but you need to change some things, like so:
CODE
{
if (speed > - 3) speed = max(-3, speed - .3)
}
Noticed how the max reverse speed is slower than the max forward speed? This is just an cosmetic choice I made because people can't really run very quickly backwards.
That's it!
Pros: Realistic movement, if you set things correctly
Cons: Can require some coding knowledge
FPS Style movementI'm not going to cover making a 3d game here, just how to make your character turn like he was in an fps. Left and Right rotate, Up moves forward, Down moves backwards.
(NOTE: If you want your character to rotate, you need to have the pro version of GM, or make a bunch of different pictures for different angles of the character).
This is an increadibly easy one, since you've already made the acceleration and friction.
Make Left and Right keyboard triggers, and set the variable 'direction' relative to a number. I'm using -5 for right, and 5 for left.
This Next Step Is For Pro Version OnlyWell, it works, but you can't see that the character is turning when he's stationary. So make an "end Step" trigger. Set the variable image_angle = direction. Now the image rotates with the angle.
Now, we're back to stuff anyone can do. Collisions in this type of movement are very difficult to do well. The best I can think of is trigger: Collision with block Action: Set variable speed = 0. But, I'd like the player to be able to slide along the wall. An official tutorial on the Yoyo Games website outlines this, but I can't quite understand how it works.
Pros: Good for games where you want finer character control
Cons: Collisions are hard to do well
The Last 2 Tutorials are for Pro Version Only!Space MovementThis movement type is similar to movement on Asteroids. It's fun, and it looks cool if you do it well.
How movement will work operates on the principle of inertia. In space, there's no friction to slow down your ship, so it will go at a constand speed if it can. You can rotate your ship freely as it moves. Pressing up will start it moving in the direction its facing, but it has to fight inertia. Pressing down will cause the ship to decelerate.
For this one, I've made a new sprite that looks nicer.

And an enemy.

And I've made a background for the game.

I've decided for this one that I don't want a top speed for the ship. So it's a bit different. On the keyboard up trigger, make a move free action (the blue arrows) and set the move speed relative to .2 and the direction to image_angle
For left and right, add and subtract from image_angle respectively. I've found that 3 is a good interval. It's probably best to have an interval that goes evenly into 360. Moving down is easy: check if the speed is higher than 0. If so, decrease the speed by .3. If the speed is less than 0, set the speed to 0. This movement is incredibly easy to do, and looks really nice.
In my game, I've added particle effects, enemies, and a weapon.

The bullets wrap around the screen, as does the player.
Car MovementThis is the one that took me the most time to implement. First, there's a couple things you have to understand about how ground vehicles move:
1. Ground vehicles can't rotate while standing still
2. Ground vehicles turn based on wheel angle, not on direction of thrust
3. The wheels tend to reset their position when the car is moving.
So, I've made a car graphic for this game. It's basically an SUV

.
Let's go ahead use the same acceleration/deceleration method from before. The forward max speed is 25, with an acceleration of .5 . For reverse, the max is -10, and the acceleration is -.5 . I've set the friction to .2 .
In the create trigger, we're going to make a new variable called varWheel. This determines the angle of the wheels. Set it to 0 for now.
Now, on the left and right keyboard events, you're going to be adjusting the angle of the wheels. What I've done on the left key trigger is make a check whether the angle is less than 5. Then if it's true, one is added to the value of varWheel. The same is true for the right key, but with negatives.
Now we need to make the turning actually work. What we'll do is this:
Make a step trigger that checks whether speed is 0. If speed <> 0, then direction = varWheel. Now, turning works. But we want the wheels to reset after the player hasn't been using the turning keys for a while. So, in this same check for if speed <> 0, subtract .5 from varWheel if it's above 0, and add .5 if it's below 0.

Ignore the friction check and the particle stuff. It's just to make it look pretty.
That should do it! Now you've got car-like movement. Of course, I can't get collisions to work yet. The best I can do is different frictions on different terrains.
Thus ends this tutorial. If you have questions, don't hesitate to ask!