SOLVED Creating a top down "Lego Star Wars like" co-op camera

G

GMJae

Guest
Hello. This is my first post so apologies if the formatting is not up to standard. I have 2 player objects on the screen. The camera is centered between them. I would like the players to be stopped when they try to go outside of the camera, like Lego Star Wars co-op, but top down. Here is the code I have so far:

GML:
//Camera Step Event
following = [obj_player1, obj_player2];

x += (xTo - x);
y += (yTo - y);

xTo = (following[0].x + following[1].x) / 2;
yTo = (following[0].yCenter + following[1].yCenter) / 2;

camera_set_view_pos(view_camera[0], x - RESOLUTION_WIDTH / 2, y - RESOLUTION_HEIGHT / 2);

//Player Camera Border Code
camRightBorder = camera_get_view_width(view_camera[0]) + camera_get_view_x(view_camera[0]);
camLeftBorder = camera_get_view_x(view_camera[0]);
camDownBorder = camera_get_view_height(view_camera[0]) + camera_get_view_y(view_camera[0]);
camUpBorder = camera_get_view_y(view_camera[0]);
   
x = clamp(x, camLeftBorder + sprite_width, camRightBorder - sprite_width);
y = clamp(y, camUpBorder + sprite_height, camDownBorder - sprite_height);
This works for left, right and down, but for some reason when I move up instead of stopping the moving player it drags the other with it. Anyone have a fix? Let me know if you guys need more clarification. Thank you in advance.

("yCenter" is the middle of the player object, since I have the sprite origin at the bottom. When I try just using "y", it drags towards the bottom instead).
 
Last edited by a moderator:
G

GMJae

Guest
While testing, I have now realized the only border working is the bottom. The rest drag the other player to the moving player.
 
G

GMJae

Guest
I tried toying around with other ideas and found one that works pretty well.
GML:
//Player Movement Script
camRightBorder = camera_get_view_width(view_camera[0]) + camera_get_view_x(view_camera[0]);
camLeftBorder = camera_get_view_x(view_camera[0]);
camDownBorder = camera_get_view_height(view_camera[0]) + camera_get_view_y(view_camera[0]);
camUpBorder = camera_get_view_y(view_camera[0]);

if (hspd > 0) && (bbox_right < camRightBorder) {
    x += hspd;   
}
if (hspd < 0) && (bbox_left > camLeftBorder) {
    x += hspd;   
}
if (vspd > 0) && (bbox_bottom < camDownBorder) {
    y += vspd;   
}
if (vspd < 0) && (bbox_top > camUpBorder) {
    y += vspd;   
}
 
Top