GMS 2 How do I make the camera follow the player

Kyndle

Member
I have a basic 400x240 room, that's gonna be how much of the room the player can see, I had viewports in my other game-thing, but it doesn't work like I want it to.
I want the camera to follow the player in a 1200x240 room, when I do the enable viewports thing and make viewport 0 follow obj_player, it seems like after a certain number (probably 200 since that's half the screen) no matter what I put to Horizontal Border the player seems to be centered (well, the player's origin is centered, so the player). I want the player character to be close to the left side of the screen so the actual player can see what's ahead of them. Kind of like how the player can see the what's coming at them in Bit.Trip Runner
 
Many ways to do this. If you're using the viewport 0 to follow the player, you could simply offset the x-axis.

What is the width of your viewport?

say it was 640. set your x pos of the viewport to -288 - for a player sprite of 32x32 pixels. This will have the viewport show the player at the far left of the screen with a little bit of space behind to the left.

why -288?

Half of 640 = 320 which is the centre point, -32 pixel sprite = 288
 
Last edited:

Kyndle

Member
Many ways to do this. If you're using the viewport 0 to follow the player, you could simply offset the x-axis.

What is the width of your viewport?

say it was 640. set your x pos of the viewport to -288 - for a player sprite of 32x32 pixels. This will have the viewport show the player at the far left of the screen with a little bit of space behind to the left.

why -288?

Half of 640 = 320 which is the centre point, -32 pixel sprite = 288
I have no idea what you're talking about width is 400, I don't want the player to be in the center of the screen, it already does that on it's own
 

Kyndle

Member
I set the Horizontal Border to 200, the player is in the middle of the screen.
I set the Horizontal Border to 30000000, the player is still in the middle of the screen.
How do I make the camera move while the player is near the left side of the screen, not middle
 

ophelius

Member
Is this for a side scroller type game where he's moving to the right? You want the player to be positioned more to the left while looking to the right with more of the level shown in front of him than behind him?

Edit: Never mind, I read more of your original post.

So if you want to player to be stationed more on the left side of the screen with more of the level showing in front of him, you'll have to make a camera object and control the camera through code. The camera follow system GM comes with is very basic and doesn't allow what you're describing. So I would suggest you turn it off and code a camera yourself.

Camera's create event:
Code:
CamW = camera_get_view_width(view_camera[0]);  //we'll need the camera width
Offset = 30;  //How many pixels ahead to shift the camera from the centre
Camera's step event:
Code:
camera_set_view_pos(view_camera[0], Player.x - (CamW / 2) + Offset, 0);
or simpler(which you then wouldn't need the CamW variable):
Code:
camera_set_view_pos(view_camera[0], Player.x - Offset, 0);
This is untested
 
Last edited:
I have no idea what you're talking about width is 400, I don't want the player to be in the center of the screen, it already does that on it's own
Ok viewport width is 400.
Viewport X POS which is directly above viewport width will likely be 0.

Change it from zero to -168.

If you want to write out code for a custom camera, which is probably better practice and likely more helpful in the long run, consider something like above. If you don't understand any of that code, just head to YouTube and look for game maker camera setups.

Hope you manage to get what you're after.
 

Yal

šŸ§ *penguin noises*
GMC Elder
One simple approach is to not make the view follow the player directly, but create a "view centerpoint" object which always moves ahead of the player, and have the view follow that.
 

Kyndle

Member
Is this for a side scroller type game where he's moving to the right? You want the player to be positioned more to the left while looking to the right with more of the level shown in front of him than behind him?

Edit: Never mind, I read more of your original post.

So if you want to player to be stationed more on the left side of the screen with more of the level showing in front of him, you'll have to make a camera object and control the camera through code. The camera follow system GM comes with is very basic and doesn't allow what you're describing. So I would suggest you turn it off and code a camera yourself.

Camera's create event:
Code:
CamW = camera_get_view_width(view_camera[0]);  //we'll need the camera width
Offset = 30;  //How many pixels ahead to shift the camera from the centre
Camera's step event:
Code:
camera_set_view_pos(view_camera[0], Player.x - (CamW / 2) + Offset, 0);
or simpler(which you then wouldn't need the CamW variable):
Code:
camera_set_view_pos(view_camera[0], Player.x - Offset, 0);
This is untested
Thanks for the help, it works
Here's a good guide to GMS2 camera system, also covers following the player:

Thanks for the video, I checked online but the others wasn't as good as this one
 
Top