• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Camera follow player?

G

gamedev513

Guest
Hey guys,

I am using GMS2 and I have a working view script that is pixel perfect. Now, I'm trying to get my camera to follow the player in both the X and Y direction (i.e. top-down) when the obj_player moves, however, I haven't had much luck on getting it to work. The code below is in the CREATE event of an object called obj_camera that is created when the game launches:

Code:
#macro DEFAULT_SCALE              2
#macro DEFAULT_WIN_WIDTH     720
#macro DEFAULT_WIN_HEIGHT    540

// Object to follow
follow = obj_player;

// Resize and center window
if (!window_get_fullscreen())
{
    window_set_size(DEFAULT_WIN_WIDTH, DEFAULT_WIN_HEIGHT);
    window_set_position(round(display_get_width() div 2 - DEFAULT_WIN_WIDTH div 2), round(display_get_height() div 2 - DEFAULT_WIN_HEIGHT div 2));
}
// Create camera
camera = camera_create();
camera = camera_create_view(0, 0, DEFAULT_WIN_WIDTH / DEFAULT_SCALE, DEFAULT_WIN_HEIGHT / DEFAULT_SCALE, 0, follow, 5, 5, 48, 48);

// Set viewport
view_set_wport(0, DEFAULT_WIN_WIDTH / DEFAULT_SCALE);
view_set_hport(0, DEFAULT_WIN_HEIGHT / DEFAULT_SCALE);

// Set viewport and camera PER ROOM.
if (room <= 0)
{
    var i = true;
    if (i == true)
    {
        var rm = room_next(room);
        for (rm = 0; rm < room_last + 1; rm++)
        {
            room_set_width(rm, DEFAULT_WIN_WIDTH / DEFAULT_SCALE);
            room_set_height(rm, DEFAULT_WIN_HEIGHT / DEFAULT_SCALE);
            room_set_view_enabled(rm, true);
            room_set_viewport(rm, 0, true, 0, 0, DEFAULT_WIN_WIDTH / DEFAULT_SCALE, DEFAULT_WIN_HEIGHT / DEFAULT_SCALE);
            room_set_camera(rm, 0, camera);                       
        }
        i = false;
    }
}

// Resize surface
surface_resize(application_surface, view_get_wport(camera), view_get_hport(camera));

// Set follow target
camera_set_view_target(camera, follow);
 
Top