Certainly! Below is a step-by-step guide to creating a toggleable camera system in GameMaker Studio 2 using GameMaker Language (GML). Pressing the V key will switch the camera between a zoomed-in view that follows the hero and a zoomed-out view that displays the entire room.
Overview
- Variables Initialization: Set up variables to manage the camera states and zoom levels.
- Input Handling: Detect when the V key is pressed to toggle the camera state.
- Camera Adjustment: Change the camera's view size and position based on the current state (zoomed in or out).
- Camera Following: When zoomed in, ensure the camera follows the hero smoothly.
Step-by-Step Implementation
1. Create a Controller Object
It's good practice to handle camera controls within a separate controller object to keep your hero object clean. Let's create an object named obj_camera_controller.
- Create Event: Initialize necessary variables.
// obj_camera_controller - Create Event
// Reference to the main camera
camera_main = camera_get_active();
// Flags whether the camera is zoomed in
zoomed_in = false;
// Define zoom levels
// Adjust these values based on your room size and desired zoom levels
zoom_out_width = room_width; // Viewport size when zoomed out (entire room)
zoom_out_height = room_height;
zoom_in_width = 320; // Viewport size when zoomed in
zoom_in_height = 240;
// Initially set the camera to zoomed out
camera_set_view_size(camera_main, zoom_out_width, zoom_out_height);
camera_set_view_pos(camera_main, 0, 0);
// Optionally, link the camera to the hero initially if you want
// Assuming your hero object is named obj_hero
// This linkage will be handled in the Step Event
2. Handle the Toggle Input and Camera Behavior
- Step Event: Detect key presses and adjust the camera accordingly.
// obj_camera_controller - Step Event
// Toggle zoom when the V key is pressed
if (keyboard_check_pressed(ord('V'))) {
zoomed_in = !zoomed_in; // Toggle the zoom state
if (zoomed_in) {
// Switch to zoomed-in view
camera_set_view_size(camera_main, zoom_in_width, zoom_in_height);
} else {
// Switch to zoomed-out view
camera_set_view_size(camera_main, zoom_out_width, zoom_out_height);
}
}
// Update the camera position based on the current zoom state
if (zoomed_in) {
// Ensure we have a reference to the hero object
var hero = instance_find(obj_hero, 0);
if (hero != noone) {
// Calculate the new camera position to center on the hero
var cam_x = hero.x - (camera_get_view_width(camera_main) / 2);
var cam_y = hero.y - (camera_get_view_height(camera_main) / 2);
// Clamp the camera position to the room boundaries
cam_x = clamp(cam_x, 0, room_width - camera_get_view_width(camera_main));
cam_y = clamp(cam_y, 0, room_height - camera_get_view_height(camera_main));
// Set the new camera position
camera_set_view_pos(camera_main, cam_x, cam_y);
}
} else {
// When zoomed out, position the camera to show the entire room
camera_set_view_pos(camera_main, 0, 0);
}
3. Ensure the Hero Object Exists
Make sure your hero object is named obj_hero or adjust the instance_find line accordingly if it's named differently.
4. Add the Controller to the Room
Don't forget to place the obj_camera_controller in your room. This object will manage the camera behavior independently of the hero and other game objects.
Additional Enhancements
Smooth Camera Transitions
For a smoother transition between zoom levels, you can interpolate the camera's view size and position over time instead of switching instantly. Here's a basic example using linear interpolation (lerp):
// Add these variables in the Create Event
transition_speed = 0.1; // Adjust for faster or slower transitions
// Modify the Step Event for smooth transitions
if (keyboard_check_pressed(ord('V'))) {
zoomed_in = !zoomed_in;
}
if (zoomed_in) {
// Target zoom-in view size
var target_width = zoom_in_width;
var target_height = zoom_in_height;
} else {
// Target zoom-out view size
var target_width = zoom_out_width;
var target_height = zoom_out_height;
}
// Smoothly interpolate the view size
var current_width = camera_get_view_width(camera_main);
var current_height = camera_get_view_height(camera_main);
var new_width = lerp(current_width, target_width, transition_speed);
var new_height = lerp(current_height, target_height, transition_speed);
camera_set_view_size(camera_main, new_width, new_height);
// Handle camera positioning as before
if (zoomed_in) {
var hero = instance_find(obj_hero, 0);
if (hero != noone) {
var cam_x = hero.x - (new_width / 2);
var cam_y = hero.y - (new_height / 2);
cam_x = clamp(cam_x, 0, room_width - new_width);
cam_y = clamp(cam_y, 0, room_height - new_height);
// Smoothly interpolate the camera position
var current_cam_x = camera_get_view_x(camera_main);
var current_cam_y = camera_get_view_y(camera_main);
cam_x = lerp(current_cam_x, cam_x, transition_speed);
cam_y = lerp(current_cam_y, cam_y, transition_speed);
camera_set_view_pos(camera_main, cam_x, cam_y);
}
} else {
// Smoothly move back to (0,0) when zoomed out
var current_cam_x = camera_get_view_x(camera_main);
var current_cam_y = camera_get_view_y(camera_main);
var new_cam_x = lerp(current_cam_x, 0, transition_speed);
var new_cam_y = lerp(current_cam_y, 0, transition_speed);
camera_set_view_pos(camera_main, new_cam_x, new_cam_y);
}
Handling Multiple Cameras
If your game uses multiple cameras, ensure that camera_main references the correct camera you intend to control. You might need to create a specific camera for this purpose using camera_create() and assign it accordingly.
Edge Cases
- Hero at Room Boundaries: The
clamp function ensures the camera doesn't show areas outside the room when the hero is near the edges.
- Room Size vs. Zoom Levels: Adjust
zoom_in_width and zoom_in_height according to your room's size and desired zoom effect to prevent the camera from exceeding room bounds.
Complete Example
Here's the complete code consolidated for the obj_camera_controller:
// obj_camera_controller - Create Event
// Reference to the main camera
camera_main = camera_get_active();
// Flags whether the camera is zoomed in
zoomed_in = false;
// Define zoom levels
// Adjust these values based on your room size and desired zoom levels
zoom_out_width = room_width; // Viewport size when zoomed out (entire room)
zoom_out_height = room_height;
zoom_in_width = 320; // Viewport size when zoomed in
zoom_in_height = 240;
// Initially set the camera to zoomed out
camera_set_view_size(camera_main, zoom_out_width, zoom_out_height);
camera_set_view_pos(camera_main, 0, 0);
// Optionally, set initial camera following here if desired
// obj_camera_controller - Step Event
// Toggle zoom when the V key is pressed
if (keyboard_check_pressed(ord('V'))) {
zoomed_in = !zoomed_in; // Toggle the zoom state
if (zoomed_in) {
// Switch to zoomed-in view
camera_set_view_size(camera_main, zoom_in_width, zoom_in_height);
} else {
// Switch to zoomed-out view
camera_set_view_size(camera_main, zoom_out_width, zoom_out_height);
}
}
// Update the camera position based on the current zoom state
if (zoomed_in) {
// Ensure we have a reference to the hero object
var hero = instance_find(obj_hero, 0);
if (hero != noone) {
// Calculate the new camera position to center on the hero
var cam_x = hero.x - (camera_get_view_width(camera_main) / 2);
var cam_y = hero.y - (camera_get_view_height(camera_main) / 2);
// Clamp the camera position to the room boundaries
cam_x = clamp(cam_x, 0, room_width - camera_get_view_width(camera_main));
cam_y = clamp(cam_y, 0, room_height - camera_get_view_height(camera_main));
// Set the new camera position
camera_set_view_pos(camera_main, cam_x, cam_y);
}
} else {
// When zoomed out, position the camera to show the entire room
camera_set_view_pos(camera_main, 0, 0);
}
Placement in the Room
- Add
obj_camera_controller: Drag and drop the obj_camera_controller into your room.
- Ensure Hero Object Exists: Make sure the hero object (
obj_hero in this example) is present in the room.
Testing
Run your game and press the V key. The camera should toggle between following the hero closely and displaying the entire room. Ensure that the hero remains visible and the camera doesn't show any unintended areas outside the room boundaries.
Customization
- Zoom Levels: Adjust
zoom_in_width, zoom_in_height, zoom_out_width, and zoom_out_height to match your game's aesthetic and room sizes.
- Transition Speed: If implementing smooth transitions, tweak the
transition_speed variable to achieve the desired effect.
- Multiple Heroes or Objects: If multiple objects need to influence the camera, enhance the logic to handle such cases.
Feel free to customize the code further to fit the specific needs of your game!