I built a camera snap system that provides flexible Mega Man style camera transitions between screens within the same room. Figured I’d share my Gamemaker Studio 2 code for anyone who wants to use a similar mechanic in their games.
I used Shawn Spalding’s tutorial on cameras in platformers, then built additional logic into my camera object to achieve the snap effect. My camera snaps to a camera snap object (an invisible yellow dotted outline that’s the same size as my viewport named “oCameraSnap”). So if I just drag and drop it into my room, it shows an outline of how the camera will snap once the player enters the area within the yellow dashed lines.
Here’s the code I use in my camera snap object. Make sure you have the variable named “follow” to equal the player object (sorry if this code’s not super optimized, I’m a designer first, developer second):
if instance_exists(oPlayer) { with (oPlayer) { // Check if in a camera snap. If so, follow the snap, not the player. var current_snap; current_snap = instance_place(x, y, oCameraSnap); if (current_snap != noone) { other.follow = current_snap; } else //If not in a snap, follow the player. { other.follow = self; } } } Camera Logic------------- //Update Destination if (instance_exists(follow)) { xTo = follow.x; yTo = follow.y; } // Update Camera Position var deltax = (xTo-x); var deltay = (yTo-y); //Prevent camera from slowly drifting when close to player if (abs(deltax) < 3) deltax = 0; if (abs(deltay) < 3) deltay = 0; x += deltax/15; y += deltay/15; x = clamp(x,min_x+view_w_half,max_x-view_w_half); y = clamp(y,view_h_half+buff,room_height-view_h_half-buff); // Update Camera View camera_set_view_pos(cam,x-view_w_half,y-view_h_half);
You can see the camera snap mechanic in action here. If you’re struggling to implement it in your own game, message me on Facebook. I’d be happy to help ya out.
Request a Demo
I’m always looking for playtesters. Let me know if you’d like to playtest level 1 and I’ll send you a demo! All I ask for is your candid feedback.