Smooth Cameras in GameMaker studio

We’ve all been there. When making your first game you will likely use:

This does provide a somewhat functional camera that follows a specific object such as the player. However, this should be set as a temporary camera.

Your current camera (if you even have one set up) likely moves in a slow, jagged, and weird way. This camera that we will create will completely fix those issues!

How should we go about making a smooth camera? For one, make sure you change the camera position through the use of GML (GameMaker Language).

Now it’s time to code!

Create

				
					if(instance_exists(oPlayer)){
	following = oPlayer;
}else{
    following = <place your default object here>
}

freeze = false;
				
			

In the create event, we just established the target that our camera will follow. This is very important since it allows us to easily switch targets. For example, I can have the camera follow a fixed point if the character is nowhere to be found.

STEP

				
					// This is your camera's following speed
var spd = 0.1;


if(!freeze and instance_exists(following)){
	var view = view_camera[0];
	var cam_x = camera_get_view_x(view);
	var cam_y = camera_get_view_y(view);
	var fol_x = following.x - gui_width/2;
	var fol_y = following.y - gui_height/2;

	new_x = lerp(cam_x, fol_x, spd);
	new_y = lerp(cam_y, fol_y, spd);
	
	//Check for Room Edge
	new_x = clamp(new_x, 0, room_w-gui_width);
	new_y = clamp(new_y, 0, room_h-gui_height);
	
	camera_set_view_pos(view, new_x, new_y);
}
if(keyboard_check_pressed(ord("F"))){ freeze = !freeze;	}
				
			

In this small script, we first must define the speed at which the camera follows the target. This can be changed to allow slow pans for cutscenes and more.

We then check to make sure that the camera isn’t frozen and that our target actually exists. If the camera is frozen (toggled with [F]) then the camera will not move. Otherwise, the camera will follow it’s target.

We define a few variables for movement that allow the camera to understand it’s position as well as the dimensions of the game window.

After this, we restrict the camera to stay in the room using clamp. Clamping ensures we don’t see things outside of the room. Remove this segment if you want the camera to freely move with the target.

Finally, we set the position of the camera!

CONCLUSION

Once you are finished with this object, plop it into the room and watch as your ugly camera turns into an amazing camera that is more customizable!

Questions

Sometimes we don’t fully explain things as well as you might want. Not to fear! We will answer all of your questions regarding any tutorial. Fill out the form below and we will try to reach back out to you as quickly as possible. Expect to hear a response within one week. For an instant response, join our discord server.

Comment

Leave a Reply

Your email address will not be published. Required fields are marked *