• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2 [SOLVED] Camera moved by pushing the screen's edge with mouse


There are many great tutorials for an easy camera setup (for example:

), but in these the camera follows either the obj_player or the mouse, while I need a mostly static view that is only move if the mouse is at or near one of the screen edges or a WASD buttons pressed (like in most top-down strategy games).

I have two theoretical solutions.

1.: Use a regular camera setup (like the above tutorials), than make a rectangle from view*0.1 to view*0.9 or something in both X and Y coordinates and if the mouse is NOT colliding with the rectange, than move as normal.

2.: Use 4 separate object (left, right, top, bottom) and if the mouse is collide with one of them, than move the camera in that direction.

My question is: what is the better idea (and why) or is there a much better/easier/less problematic solution?

(Please keep in mind that I am trying to do this as simple as possible).


I tought I understanded the idea, but I was mistaken, so one more question: how to do this exacly?

My goal is the same: the camera must stay static, unless the mouse is near one of the edges, than move to that direction.
(for example move, if outside the window_width*0.1, window_height*0.1, window_height*0.9, window_width*0.9 area)

My idea is based around 2 objects; a camera, what makes everything and an "aim", that is just a blank object (basicly it is the obj_player), that get pushed around via the mouse and the camera follows it.

My question is: how to move this obj_player?

This is a basic camera:

Create event
#macro RES_W 320     //Resolution
#macro RES_H 180
#macro RES_SCALE 3

#macro CAM_SMOOTH 0.1

view_enabled = true;     // Enable views
view_visible[0] = true;

camera = camera_create_view(0, 0, RES_W, RES_H);     // Create camera

view_set_camera(0, camera);

window_set_size(RES_W * RES_SCALE, RES_H * RES_SCALE);     // Resize window & application surface
surface_resize(application_surface, RES_W * RES_SCALE, RES_H * RES_SCALE);

display_set_gui_size(RES_W, RES_H);

var display_width = display_get_width();    // Center window
var display_height = display_get_height();

var window_width = RES_W * RES_SCALE;
var window_height = RES_H * RES_SCALE;

window_set_position(display_width/2 - window_width/2, display_height/2 - window_height/2);

Step event
var camX = camera_get_view_x(camera);
var camY = camera_get_view_y(camera);
var camW = camera_get_view_width(camera);
var camH = camera_get_view_height(camera);

var targetX = obj_player.x - camW/2;     // Set target camera position
var targetY = obj_player.y - camH/2;

targetX = clamp(targetX, 0, room_width - camW);      // Clamp the target to room bounds
targetY = clamp(targetY, 0, room_height - camH);

camX = lerp(camX, targetX, CAM_SMOOTH);      // Smoothly move the camera to the target position
camY = lerp(camY, targetY, CAM_SMOOTH);

camera_set_view_pos(camera, camX, camY);     // Apply camera position
camera_set_view_size(camera, camW, camH);

mouse_x_previous = device_mouse_x_to_gui(0);    //Store previous
mouse_y_previous = device_mouse_y_to_gui(0);

Ok, now the question part. The obj_player step event could be something like this:
but what exactly? (This has to be in the GUI layer/relative to the view/applications surface).

Alternatively the middle region could be blank and I could do four rectangles (one for each sides) and use something like this:
if (point_in_rectangle(mouse_x,mouse_y... (insert region coordinates here)
     obj_player.x += 5;     //or -5 both for the X and Y values
But how?
I tried it a few times with different tactics, but I'm clearly missing something.

Also, is it possible to simplify this further by using only a camera, that itself get tossed around (without an object to follow?)


So with something like this in the step event?
//Move left
if (point_in_rectangle(mouse_x,mouse_y,0,0,display_get_gui_width()*0.1,display_get_gui_height()))
   obj_player.x -= 2;  

//Move right
if (point_in_rectangle(mouse_x,mouse_y,display_get_gui_width()*0.9,0,display_get_gui_width(),display_get_gui_height()))
   obj_player.x += 2;  
The left and right rectangles are correct, but still not working.