GameMaker

GameMaker

Not enough ratings
How to add collision to tilesets and objects
By Sofy
How to add simple tile and object collision to top down(up, down, left, right) movement! I noticed that there is no proper guides on how to create code I thought id try myself :3
   
Award
Favorite
Favorited
Unfavorite
Welcome!
If you like this guide feel free to add suggestions for my next one ♥️

This guide is both assuming your know the bare basics of gamemaker and or just looking for a quick easy to somewhat read guide!

Originally posted by Sofy:
if you know the basics of gamemaker (creating objects, room layers, scripts ect) skip to the CODE section :3

if your new to gamemaker I highly recommend you type the code rather then copy paste
it helps more then you think
.gif]
Setup (Objects)
For this guide I am going to use my topdown movement player controller
If you want to use it I have a guide right here!


Object
For this guide we only need the one object which I'll be calling oSolid!
you might be using objSolid or obj_Solid or even o_Solid! These are all fine!

To create your object, Go to your asset browser (by default should be on the far right of the window)

right click on the objects folder, Hover over create which should expand out a bunch of options. Then find and click object (at time of writing it should be the 5th option).
I'll be naming it oSolid feel free to do the same!


Sprites
repeat the previous actions but with sprites, Feel free to name it whatever you'd like but I'll be naming mine sSolid










Setup (Tile Sets)
To create a tileset, Go to your asset browser, right click and hover create.
Now create a new Sprite AND Tile Set

Now draw or import your tiles in your new sprite which I've named sTiles

My sprite looks something like this


Now select the sprite in your Tile Set and change your proprieties to fit your tile size & spacing!

My Tile Set looks like this


Setting up the room
Head over to your room and in your layers.

now find and create a new tile layer!
I will be naming mine "Collision" but feel free to name it whatever you like!



now setup your room with tiles spread around as well as your oSolid object!










Creating a script!
before we get started make sure to create a new script!

I'll be calling mine player_collision
Code
as of version 2023.8 we can take advantage of the new collision system feature and use a tileset for any static scenery!

And as of a recent change to GameMakers collision system, they now accept an array of object or instance ids! using these new changes we can write this:
function player_collision(){ if place_meeting(x+moveX,y, [oSolid, layer_tilemap_get_id("ROOM LAYER NAME")]) { moveX = 0; } if place_meeting(x,y+moveY, [oSolid, layer_tilemap_get_id("ROOM LAYER NAME")]) { moveY = 0; } }
if you'd like to learn how place_meeting() works, Check out the gamemaker docs[manual.gamemaker.io]

The reason why we provide an array here, is that oSolid is one of our collidable objects! We can add any object, instance or tile layer in this array!


this will check if your current x position + the amount your adding will collide!
If true then set your movement to zero!


make sure you place this function above the code that changes the x/y

what my code looks like


and hit play! if everything is correct you should now be colliding with your tileset and solid objects! :)

and were already done!! :0
You did it! ♥️
End
Thanks for reading! I hope this helped you in some way or form :3
I'm no teacher but seeing there was no proper guides I knew I had to try!

If you found this helpful please like and leave a rating!
and feel free to ask any questions in the comments or suggest a new guide!

if you need any issues with your code feel free to ask in the comments too 💜

.gif]
4 Comments
Chiper_ZR 29 Jul, 2024 @ 2:38am 
@Sofy

Thanks for the Tip, im really Thankfull :)
Sofy  [author] 27 Jul, 2024 @ 10:33pm 
@Chiper_ZR

> function player_collision()

this function is meant to be added to a script :)
though you can just run the script contents in player objects step event if that makes it easier for you
Chiper_ZR 27 Jul, 2024 @ 8:09am 
is the Code Supposed to add to the Script or in the Osolid Object or am i missing something??
MaviPlayz 10 May, 2024 @ 1:14am 
noice