How To Move And Collide In GameMaker (2024)

This is the easiest way to move in GameMaker, with proper collisions, which only takes a few minutes to set up.

This will be the end result:

How To Move And Collide In GameMaker (1)

We’ll get there in 3 easy steps. You can also follow along the video:

How to create an asset?

Scroll down to Step 1 if you already know how.

If you haven’t used GameMaker before, you should know how to create assets.

GameMaker has the “Asset Browser” on the right, which lists all your assets.

How To Move And Collide In GameMaker (2)

At the top you have a (+) button – click on this and a menu will open:

How To Move And Collide In GameMaker (3)

Here, double click on the type of asset you want to create – in this tutorial we will use sprites and objects.

After an asset is made, it will show up in your Asset Browser, where you can rename it and double click to edit it.

Step 1: Sprites

Create a new project, and create two sprites: spr_player and spr_wall.

One is for the player, and the other for walls that will stop the player.

You can use “Edit Image” to draw them within GameMaker, or use “Import” to import PNG files – download mine here.

How To Move And Collide In GameMaker (4)

If you plan to rotate your walls, make sure to set its collision mask to “Rectangle with Rotation”:

How To Move And Collide In GameMaker (5)

Step 2: Objects

Now create two objects: obj_player and obj_wall.

Give them the spr_player and spr_wall sprites, respectively.

How To Move And Collide In GameMaker (6)

Before coding anything, let’s put these in the room.

In your Asset Browser, open the “Rooms” group and double click on Room1.

Drag obj_player into the room, and also place some walls.

How To Move And Collide In GameMaker (7)

You can select obj_wall, and then hold down Alt (or Option on Mac) to paint walls in the room.

I will also be stretching and rotating some of my walls to make a tiny level blockout:

How To Move And Collide In GameMaker (8)

For my wall sprites, I’m using Nine Slice so the pattern repeats instead of stretching.

Step 3: Player

Let’s program the player to move. Double click on obj_player in your Asset Browser.

In your Object Editor, see the “Events” window – here, press “Add Event”:

How To Move And Collide In GameMaker (9)

Add the Create event.

You may be asked to choose between GML Code and GML Visual. You can select either as this tutorial shows both.

Make sure to enable “Don’t ask again for this project”.

Create Event

In this event we only want to create a variable:

my_speed = 4;

How To Move And Collide In GameMaker (10)

I’m creating the variable my_speed to store how fast the player moves. You can change this to make it faster or slower.

Now, add the Step event.

How To Move And Collide In GameMaker (11)

Step Event

Here, we’ll take input from the keyboard, and move the player:

var _xinput = keyboard_check(vk_right) - keyboard_check(vk_left);var _yinput = keyboard_check(vk_down) - keyboard_check(vk_up);move_and_collide(_xinput * my_speed, _yinput * my_speed, obj_wall);

How To Move And Collide In GameMaker (12)

Here we are doing this:

  1. We get input from the left, right, up, down arrow keys
  2. We do some math to get the X direction (1 or -1) and the Y direction (1 or -1)
  3. That is passed into the move_and_collide() function
    1. Before being passed in, the X and Y directions are multiplied bymy_speed, which we defined in the Create event

Run the game and you have movement!

How To Move And Collide In GameMaker (13)

To WASD keys instead of arrow keys, replace the vk_* etc. constants with:

vk_left -> ord(“A”)

vk_right -> ord(“D”)

vk_up -> ord(“W”)

vk_down -> ord(“S”)

You can also use both arrow keys and WASD at the same time, by joining them with ‘or’ e.g. (keyboard_check(vk_left) or keyboard_check(ord(“A”)).

Limiting Movement

If you try to move diagonally (e.g. hold both right and down at the same time) and slide along a wall, you’ll notice the player moves faster than it normally would.

You can fix this by setting a limit on how fast the player can move on any axis. This is set in the last two arguments of move_and_collide().

I’ll change my move_and_collide() line/action to this:

move_and_collide(_xinput * my_speed, _yinput * my_speed, obj_wall, 4, 0, 0, my_speed, my_speed);

How To Move And Collide In GameMaker (14)

There are 5 new arguments here:

  • The initial three (4, 0, 0) are arguments I don’t want to change, so I’ve passed in their default values as per the manual.
  • The last two arguments are the X and Y speed limits of the player. I’ve passed in my_speed for both.

Now, the player won’t move faster when moving diagonally while sliding along walls.

Shape Collisions

This function supports collisions for sprites of any shape – e.g. ellipse, diamond and precise shapes:

How To Move And Collide In GameMaker (15)

Using an ellipse shape for my player

How To Move And Collide In GameMaker (16)

Using a precise shape for this diagonal wall

Throw these shapes into the game and they will work perfectly:

How To Move And Collide In GameMaker (17)

Invisible Objects

If you’re primarily using tiles for your environment, you can still continue to use objects such as obj_wall for collisions. Just make the object invisible:

How To Move And Collide In GameMaker (18)

Place your wall instances in your room to cover your tiles. These will continue to work in the game, but won’t be visible to the player.

Read the move_and_collide() manual page for more info.

Also see: Fastest Way To Make A Platformer in GameMaker

Happy GameMaking!


How To Move And Collide In GameMaker (2024)

FAQs

How to move and collide GameMaker? ›

Here we are doing this:
  1. We get input from the left, right, up, down arrow keys.
  2. We do some math to get the X direction (1 or -1) and the Y direction (1 or -1)
  3. That is passed into the move_and_collide()function. Before being passed in, the X and Y directions are multiplied by my_speed, which we defined in the Create event.

How do you move in GameMaker? ›

Remember, in a GameMaker room, to move right we add to the x position and to move left we subtract, so this code will give us a positive or negative value that we can add or subtract to move horizontally or vertically depending on the keyboard input.

What does ++ do in GameMaker? ›

++, -- are used to add or subtract one (1) from a value. It is worth noting that placing this before or after the value to be added to or subtracted from will have slightly different results. For example: ++a will increment the variable and return the incremented value.

How do you move an instance in GameMaker? ›

The two main ways of moving an instance is to either set the actual position or to set a speed/direction vector, and this can be done either using the built-in instance variables or to use specific movement functions.

What is the collision event in GameMaker? ›

Obviously when making a game, it is very important that you know when two (or more) instances of an object have collided, and for that we have the Collision Event. This is an event that you place in an object and then specify against which other object you should be checking for collisions.

What is a collision mask GameMaker? ›

A precise collision mask, where the mask will conform to the non-transparent pixels of the sprite, based on the tolerance value given (see below)) bboxkind_spine. Apart from rectangle, this is the only valid option for Spine sprites. It enables more precise collision checking against the mesh in the sprite.

What does == mean in GameMaker? ›

= should only be used if you want to set a variable equal to some other value. I think GML still accepts it in an if statement but it's bad practice to use it there. == is only used when you want to compare two separate values, like in your examples.

Is GameMaker 2 easy? ›

Yes! GameMaker Studio is relatively easy to learn compared to other game engiens like Unity or Unreal, as you can make a game without very much code or scripting.

Is GML similar to C++? ›

They're both procedural languages... a lot of people don't like to hear this, but they're all basically the same.

How do you know if two objects collide in GameMaker? ›

The simplest way to check for collisions against objects is to use place_meeting.

How to randomize GameMaker? ›

randomise
  1. Syntax: randomise(); // or. randomize();
  2. Returns: Real (unsigned 32 bit value)
  3. Example: randomise(); The above code will randomise the seed. Back: Number Functions. Next: round. © Copyright YoYo Games Ltd. 2023 All Rights Reserved.

How to code gravity in GameMaker? ›

gravity
  1. Syntax: gravity;
  2. Returns: Real (single precision floating point value)
  3. Example: if (!place_meeting(x, y + 1, obj_Ground)) { gravity = 0.01; } else. { gravity = 0; } The above code will only apply gravity if the instance does not find any instances of "obj_Ground" beneath it. Back: Instance Variables.

How do I move around my workspace in GameMaker? ›

You can switch from workspace to workspace by clicking the tab, and within each workspace you can pan around and zoom in/out to see different areas as you work. To pan simply hold down the middle mouse button or click and hold + .

How do you move multiple objects in GameMaker? ›

If you hold down shift you can select multiple objects, and then move them altogether by clicking and dragging one of them.

How to use WASD GameMaker? ›

To implement WASD movement in GameMaker, you will need to use GameMaker Language (GML) to check for keyboard input and move an instance (such as the player character) accordingly. Below is a simple code example that you could put in the Step event of your player object to achieve basic WASD movement.

How does collision work in games? ›

Almost all games use a posteriori collision detection, and collisions are often resolved using very simple rules. For instance, if a character becomes embedded in a wall, they might be simply moved back to their last known good location.

Top Articles
392 Kendale Road, Buellton, CA 93427 - MLS# 24-2117 | CENTURY 21
NHL Draft 2023: Die 1. Runde im Überblick | NHL.com/de
Metra Union Pacific West Schedule
Inducement Small Bribe
Dlnet Retiree Login
His Lost Lycan Luna Chapter 5
Explore Tarot: Your Ultimate Tarot Cheat Sheet for Beginners
Overnight Cleaner Jobs
Wannaseemypixels
Kansas Craigslist Free Stuff
R Tiktoksweets
Where does insurance expense go in accounting?
Busted Newspaper S Randolph County Dirt The Press As Pawns
Craiglist Galveston
The Banshees Of Inisherin Showtimes Near Regal Thornton Place
Tcgplayer Store
Toy Story 3 Animation Screencaps
Publix Super Market At Rainbow Square Shopping Center Dunnellon Photos
Schedule An Oil Change At Walmart
Jeff Now Phone Number
[PDF] PDF - Education Update - Free Download PDF
eugene bicycles - craigslist
Makemv Splunk
4.231 Rounded To The Nearest Hundred
Cinema | Düsseldorfer Filmkunstkinos
Will there be a The Tower season 4? Latest news and speculation
Best Laundry Mat Near Me
Puffin Asmr Leak
Haunted Mansion Showtimes Near Cinemark Tinseltown Usa And Imax
Gasbuddy Lenoir Nc
Black Adam Showtimes Near Amc Deptford 8
Asian Grocery Williamsburg Va
20 Best Things to Do in Thousand Oaks, CA - Travel Lens
Restored Republic May 14 2023
Low Tide In Twilight Manga Chapter 53
Puretalkusa.com/Amac
Wilson Tattoo Shops
Seminary.churchofjesuschrist.org
Emily Tosta Butt
Inducement Small Bribe
LumiSpa iO Activating Cleanser kaufen | 19% Rabatt | NuSkin
Craigslist St Helens
Doe mee met ons loyaliteitsprogramma | Victoria Club
Keci News
Kate Spade Outlet Altoona
Mountainstar Mychart Login
Mcoc Black Panther
Oefenpakket & Hoorcolleges Diagnostiek | WorldSupporter
6463896344
Diccionario De Los Sueños Misabueso
Game Like Tales Of Androgyny
Appsanywhere Mst
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 6562

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.