I remade my older camera tutorial that achieves very similar results in order to not use needlessly complex matrices. At the time I thought it was a good idea to show how to implement a camera from "base principles" or whatever I think, but I don't really think that's the best approach for beginners (or even just, most of the time. Leverage what GameMaker makes easy for you!) and it was causing problems for devs later down the line. Sorry about that! Here's a new, better one.
I thought I was just watching a normal tutorial video, but then you revealed that you made pokey poke! Holy shit! The movement in that game is so clean
It doesnt work, Ive looked over the code many times and I wrote it exaclty as intended Camera doesnt follow I even have the camera object in the room...
Under my Room, I checked Viewport 0 and the Visible checkbox and unchecked. I was able to see the camera properly when I did, dunno why it's unchecked. Hope this helps
Thank you so much for this quick and easy to follow tutorial. You have really made me develop so much within game making and programming. Best of luck to your channel!
Amazing as always Shaun!! Thank you so much. Agree with the other comments these short format videos are fantastic, super concise and densely packed with amazingly useful info!!!
If your camera is not centered I may have the solution. This came through a lot of trail and error and messing with settings. But it may be because your viewport dimensions and your coded camera window dimensions do not match. You can adjust viewport dimensions by double clicking on the room you are using the camera in, looking to the left side of the screen below Room Settings and Viewports and Cameras. For this example let's just say we want the screen to be 725 pixels wide and 450 pixels tall. Change the numbers in Camera Properties for 'Width' and 'Height' to match those respective numbers. After that, go to Create Event for your camera object (oCamera) and change the designated height and width to also match the height and width back in Camera Properties. If you do this and click 'Run' you should be centered. Keep in mind that if you don't have a proper aspect ratio set for those two parameters, that the objects in your game will look stretched in order to take all the available space in the window. In this case just raise or lower your width incrementally until things look about right. I hope this helps you.
thanks! this solved the problem PERFECTLY! however i dont wanna be rude but could you make a large space between the steps and comments, im worried this very good solution might look intimidating to new players due to how bunched up it is. THANKS AGAIN!!
I had the same problem, I just went ahead and followed an older version of his tutorial for his platformer series: ruclips.net/video/YbppozAV1Q8/видео.html
Hello! Listen, I see you're experienced, but I'm new and I need help. Please tell me what to do if I need to create a rotation angle limitation for an object? For example, I need the character to be able to raise the pistol a maximum of 45 degrees. How to do it? Sorry if I wrote something unclear. I do not speak English...
Old comment but If anyone is wondering about the same thing, you guys may take a look at the function "clamp" to try and fix this, it ensures a value stays on range I unfortunately don't know a "template" code to help with this
@gianstb Well, thanks anyway. Although I stopped make games for a while, this advice might come in handy if I get back to games. And sorry, maybe my english too bad.
Shaun, What would be the best way to clamp the view so that when you jump around you don’t see any black and you stay within the boundaries of the camera? … would it just be from 0 to room_with and 0 to room_height ?
The camera in this video has its anchor in the center of the view (its x and y value), so you'd actually want to clamp between camWidth / 2 and room_width - camWidth / 2 and same for height
@@jackkraus6948 I'm trying to wrap my head around this. Shot in the dark that didn't work: camera_set_view_pos(view_camera[0], clamp(x-(camWidth*0.5), camWidth*0.5, room_width - camWidth*0.5), clamp(y-(camHeight*0.5), camHeight*0.5, room_height - camHeight *0.5));
@@echopaff So you're going to want to clamp the x and y based on the room_width and room_height, then set the view position x = clamp(x, camWidth/2, room_width-camWidth/2); y = clamp(y, camHeight/2, room_height-camHeight/2); camera_set_view_pos(view_camera[0], x-camWidth/2, y-camHeight/2);
Would you know how to modify this to work better with a constantly accelerating/decelerating object? As of rn the player object will just slowly drift away from the camera if left to just fall and I’m a lil stumped 😅
Im kinda late but at the step event at line 10,it says that something is wrong about the number of arguments for function camera_set_view_pos. know how to fix it? im new btw.
Id like a camera to follow my player ship at the center of the world, and zoom in and down on him. But implement a max zoom in level, and a max level you can zoom out.
for the if (follow != (noone)) part of the camera, I used a little different worded when I was getting crashes on my camera not existing. if(instance_exists(follow)) Does this little change of wording matter?
Hi Shaun I have had a problem for like 1 year and thats when I used your old camera tutorial (I have not tried this one) but the problem I have is that when I stop moving the camera is a bit like glitchy until it finds the position to stay in is there a fix or is it just how it is?
So that'll kinda happen with a pixel art game unless you have a large "true" resolution (which has its own host of problems) if you're moving at non-integer values. Which this does because it lerps for smoothness. There's things you can do to mitigate this but its a little tricky, I might tackle this in a future video. You can try things like trimming fractions from the amount you want to move the camera and storing them up, only adding them back in when they add up to a whole number. So you move the same amount but only at integers. Even then you'll still find very slow camera movement at 1px every half second or whatever will look a little "jagged" but that really is just how true pixel resolution games kinda be.
Hello, I'm having a strange issue. Everything appears to work, however the player is stuck on the far left of the screen. It still follows smoothly, but while the camera is centred, the player is not
Just implemented it in my game. What was interesting It became laggy around middle of room for one frame, but seems it was because I've got hardcoded "camera_set_speed/camera_set_border" (leftover from previous system) and removing them fixed it (I didn't have time to investigate which was causing that camera "freeze" for one frame in one specific place).
my character doesn't move, it was moving fine, but after the grid based movement it just moves in a diagonal and then stops, I've checked and even retraced my steps and it's still not working
Add this to your step event : x = clamp(x,camera_get_view_width(view_camera[0]) * 0.5+buff,room_width - camera_get_view_width(view_camera[0]) * 0.5-buff); y = clamp(y,camera_get_view_height(view_camera[0]) * 0.5+buff,room_height - camera_get_view_height(view_camera[0]) * 0.5-buff);
Check if the x or y is NOT less or equal to the left and top sides, or greater or equal to the right and bottom. If this returns true, move camera If not, do nothing
I put a sprite on my camera, and the sprite shoots upward continually, yet the view seem to follow the player correctly. What would cause that to happen?
I did everything in the tutorial but I have a problem, when my character advances towards the end of the room the camera still advances and it is not supposed to, solution for that? Sorry if something is not understood, I'm from South America and I don't know much English :c
No that's a normal feature of this code as we didn't write anything to prevent that. Sometimes you want this for example in games with prog gen worlds etc. You can easily restrict the camera by using something like clamp() on the x and y coordinates after moving. For example x = clamp(x,0+halfcamerawidth,room_width-halfcamerawidth) and the same for y using half the camera height instead.
You'll want to use clamp to keep the camera within the boundaries of your room. It'll end up looking something like this. x = clamp(x, CameraWidthHalf, room_width - CameraWidthHalf); y = clamp(y, CameraHeightHalf, room_height - CameraHeightHalf);
Love this, Shawn! Do you think there might be a way to implement some sort of following border to the character, so the camera only moves when they get a certain distance to the camera’s perimeter?
Think of it this way: You should only change the xTo and yTo coordinates if the follow object is far away. Extending the if statement to something simple like if(follow != noone && distance_to_object(follow) > radius) would do the trick (change radius in whatever the radius should be)
Hi Shaun, I am trying to use your code for a multiplayer game I'm working on. I want a camera for each individual player that focuses on their own assigned object. Also, I implemented the code shown in this video and ran the game, but when it started, the four objects (one for each player) were all in the bottom-right corner of the camera's view. Could you help me figure out what's going on and how this can be fixed? I'm very new to all this so please keep it simple if you can, thanks. EDIT: I tried tinkering with the settings a little bit, and although I reset everything to what I originally had, the window is now incredibly small. What's going on? Thanks again.
i did all of this but my camera actually moves diagonally when the player moves , cant find what i did wrong edit: actually just rewrite all the code and it works
I managed to find a pretty simple solution. On the step event, instead of having this: xTo = follow.x; yTo= follow. y; It should be like this: x_to = clamp(follow.x, 0+(cam_width/2), room_width-(cam_width/2)); y_to = clamp(follow.y, 0+(cam_height/2), room_height-(cam_height/2)); Clamp ensures that a value stays within a min and max value, in this case being the room's start and end. By adding (width/height /2) the camera object stops while letting the viewport contintue to the border. (Sorry for flexing but I'm kinda proud for coming up with this myself.)
No problem. I'll also just mention another thing I found which might be useful for anyone reading this. When transitioning to another room the camera might briefly be outside the bounds and slide in. To prevent this, add this underneath the y_to/y_to: EDIT: The code I previously suggested doesn't work on all sides, use this code instead. x = clamp (x, (0 + cam_width/2), room_width - (cam_width/2)); y = clamp (y, (0 + cam_height/2), room_height - (cam_height/2)); This straight up tells the camera to stay within the bounds at all times.
Or can you make a video of how to put a unlimited dash move which uses the shift button but it for 2d platformer but if you want to know what pizza tower go to RUclips and search up pizza tower and one of the streams that was posted on RUclips it usually show him programming the game
Really helpful, but one thing: When I did it, it was following my character a bit too far down to the right, making my character look in the top left. Any idea why this is???? Edit: Fixed it!!! For anybody else having this problem, set BOTH 0.5's to 1.0 and your set!
I remade my older camera tutorial that achieves very similar results in order to not use needlessly complex matrices. At the time I thought it was a good idea to show how to implement a camera from "base principles" or whatever I think, but I don't really think that's the best approach for beginners (or even just, most of the time. Leverage what GameMaker makes easy for you!) and it was causing problems for devs later down the line. Sorry about that! Here's a new, better one.
I thought I was just watching a normal tutorial video, but then you revealed that you made pokey poke! Holy shit! The movement in that game is so clean
That short format is pure gold!
Love these short and to the point knowledge bursts of pretty useful information, thanks!
It doesnt work, Ive looked over the code many times and I wrote it exaclty as intended
Camera doesnt follow
I even have the camera object in the room...
Under my Room, I checked Viewport 0 and the Visible checkbox and unchecked. I was able to see the camera properly when I did, dunno why it's unchecked. Hope this helps
Create event (global variable is optional):
camera = camera_create_view(0, 0, camera_w, camera_h, 0, -1, -1, -1, 0, 0);
global.main_camera = camera;
Room Start event:
view_enabled = true;
view_visible[0] = true;
view_camera[0] = global.main_camera;
This worked for me!
This worked perfectly. I tried two other tutorials which didn't whether my character was in view or not. Yours does work. Thank you, awesome person!
Thanks!
Thank you so much for this quick and easy to follow tutorial. You have really made me develop so much within game making and programming. Best of luck to your channel!
fantastic tutorial, extremely easy to follow and straight to the point. Thank you ever so much for making this
Amazing as always Shaun!! Thank you so much. Agree with the other comments these short format videos are fantastic, super concise and densely packed with amazingly useful info!!!
Glad you enjoyed it!
If your camera is not centered I may have the solution.
This came through a lot of trail and error and messing with settings. But it may be because your viewport dimensions and your coded camera window dimensions do not match.
You can adjust viewport dimensions by double clicking on the room you are using the camera in, looking to the left side of the screen below Room Settings and Viewports and Cameras. For this example let's just say we want the screen to be 725 pixels wide and 450 pixels tall. Change the numbers in Camera Properties for 'Width' and 'Height' to match those respective numbers. After that, go to Create Event for your camera object (oCamera) and change the designated height and width to also match the height and width back in Camera Properties. If you do this and click 'Run' you should be centered.
Keep in mind that if you don't have a proper aspect ratio set for those two parameters, that the objects in your game will look stretched in order to take all the available space in the window. In this case just raise or lower your width incrementally until things look about right. I hope this helps you.
Thanks!
thanks! this solved the problem PERFECTLY!
however i dont wanna be rude but could you make a large space between the steps and comments, im worried this very good solution might look intimidating to new players due to how bunched up it is.
THANKS AGAIN!!
@@grigla2235 Can do. Glad I was able to help.
Holy guacamole! You've earned a subscriber!
Thanks man! This worked perfectly first try. Super quick way to set up an epic camera. 10/10
This is a great video! It helped a lot!
However, how do I make it stop when it intersects with the Room Boundary?
You have helped me so much in my career of coding so thanks!
idk why but the camera wont move to the player, i tried everything but it wont work.
did u put the camera object into the room?
@@mfyz_0 yes
Did you even find a fix for this? I have the same problem. Quadruple checked the code, cameras in room etc.
I had the same problem, I just went ahead and followed an older version of his tutorial for his platformer series: ruclips.net/video/YbppozAV1Q8/видео.html
wow this worked thanks~ looks awesome now
Hello! Listen, I see you're experienced, but I'm new and I need help. Please tell me what to do if I need to create a rotation angle limitation for an object? For example, I need the character to be able to raise the pistol a maximum of 45 degrees. How to do it? Sorry if I wrote something unclear. I do not speak English...
Old comment but
If anyone is wondering about the same thing, you guys may take a look at the function "clamp" to try and fix this, it ensures a value stays on range
I unfortunately don't know a "template" code to help with this
@gianstb Well, thanks anyway. Although I stopped make games for a while, this advice might come in handy if I get back to games. And sorry, maybe my english too bad.
@@-Russian_Knight It's ok man, and don't worry, your english is very good
@gianstb Thank you ❤️
Shaun,
What would be the best way to clamp the view so that when you jump around you don’t see any black and you stay within the boundaries of the camera? … would it just be from 0 to room_with and 0 to room_height ?
The camera in this video has its anchor in the center of the view (its x and y value), so you'd actually want to clamp between camWidth / 2 and room_width - camWidth / 2 and same for height
@@jackkraus6948 I'm trying to wrap my head around this. Shot in the dark that didn't work:
camera_set_view_pos(view_camera[0], clamp(x-(camWidth*0.5), camWidth*0.5, room_width - camWidth*0.5), clamp(y-(camHeight*0.5), camHeight*0.5, room_height - camHeight *0.5));
@@echopaff So you're going to want to clamp the x and y based on the room_width and room_height, then set the view position
x = clamp(x, camWidth/2, room_width-camWidth/2);
y = clamp(y, camHeight/2, room_height-camHeight/2);
camera_set_view_pos(view_camera[0], x-camWidth/2, y-camHeight/2);
@@jackkraus6948 where would you implement this code
This was very helpful, ty.@@jackkraus6948
how do I stop the camera at the edge of the room
What if you want the camera to focus on more than one object like, the two players in a fighting game?
Would you know how to modify this to work better with a constantly accelerating/decelerating object? As of rn the player object will just slowly drift away from the camera if left to just fall and I’m a lil stumped 😅
I love the video but none of the code works in the GameMaker studio 1.4 for some odd reason
This is wonderful! How would you go about zooming in and out the camera though?
how can i limit the camera movement so it doesnt gets out of the room?
can u also make it using GM visual?
Im kinda late but at the step event at line 10,it says that something is wrong about the number of arguments for function camera_set_view_pos.
know how to fix it?
im new btw.
Id like a camera to follow my player ship at the center of the world, and zoom in and down on him. But implement a max zoom in level, and a max level you can zoom out.
Great tutuorial! But how do you make the camera stop when it reaches the edge of the room?
for the if (follow != (noone)) part of the camera, I used a little different worded when I was getting crashes on my camera not existing.
if(instance_exists(follow)) Does this little change of wording matter?
Hi Shaun I have had a problem for like 1 year and thats when I used your old camera tutorial (I have not tried this one) but the problem I have is that when I stop moving the camera is a bit like glitchy until it finds the position to stay in is there a fix or is it just how it is?
So that'll kinda happen with a pixel art game unless you have a large "true" resolution (which has its own host of problems) if you're moving at non-integer values. Which this does because it lerps for smoothness.
There's things you can do to mitigate this but its a little tricky, I might tackle this in a future video. You can try things like trimming fractions from the amount you want to move the camera and storing them up, only adding them back in when they add up to a whole number. So you move the same amount but only at integers. Even then you'll still find very slow camera movement at 1px every half second or whatever will look a little "jagged" but that really is just how true pixel resolution games kinda be.
@@SaraSpalding Okay thanks for the quick response!
This is great, but how do I make the camera not go off screen and show all my mechanisms out there in the void? lol
everything becomes streched when i finished coding the camera? this also happened when i followed a diferent tutorial, please help
Hello, I'm having a strange issue. Everything appears to work, however the player is stuck on the far left of the screen. It still follows smoothly, but while the camera is centred, the player is not
same, did you find a fix?
somehow my camera falls down instantly. I dont have physics enabled and I am doing a top down game. would you have any idea why this happens?
Just implemented it in my game.
What was interesting It became laggy around middle of room for one frame, but seems it was because I've got hardcoded "camera_set_speed/camera_set_border" (leftover from previous system) and removing them fixed it (I didn't have time to investigate which was causing that camera "freeze" for one frame in one specific place).
my character doesn't move, it was moving fine, but after the grid based movement it just moves in a diagonal and then stops, I've checked and even retraced my steps and it's still not working
nice video, thanks for this good content, finally i found the solution of my problem,
again, thanks you
The camera follow works perfectly, but is there a way to make the camera object not go out of the room's bounds?
Add this to your step event :
x = clamp(x,camera_get_view_width(view_camera[0]) * 0.5+buff,room_width - camera_get_view_width(view_camera[0]) * 0.5-buff);
y = clamp(y,camera_get_view_height(view_camera[0]) * 0.5+buff,room_height - camera_get_view_height(view_camera[0]) * 0.5-buff);
@@tym4971 thanks! it's working
So! I got it to work, but it doesn’t zoom in as close as I need it to, what do I do?
Correction, not ANY GameMaker, camera-set is only for Gamemaker 2.
mine is not on the center
same
same
Using this camera code in a 2d platformer, how would I stop the camera from exiting the boundaries of the room?
Check if the x or y is NOT less or equal to the left and top sides, or greater or equal to the right and bottom.
If this returns true, move camera
If not, do nothing
I put a sprite on my camera, and the sprite shoots upward continually, yet the view seem to follow the player correctly. What would cause that to happen?
I have a screen shake sprite, and whenever the screen shakes, the camera turns off. how do I fix this?
oh damn thanks a lot bro
I did everything in the tutorial but I have a problem, when my character advances towards the end of the room the camera still advances and it is not supposed to, solution for that?
Sorry if something is not understood, I'm from South America and I don't know much English :c
What I'm trying to say is that on camera it shows me parts of the room that are empty
No that's a normal feature of this code as we didn't write anything to prevent that. Sometimes you want this for example in games with prog gen worlds etc. You can easily restrict the camera by using something like clamp() on the x and y coordinates after moving. For example x = clamp(x,0+halfcamerawidth,room_width-halfcamerawidth) and the same for y using half the camera height instead.
How can I zoom in and out in DnD? I found a function called "Set View Variable, but can't seem to do anything with it."
anyone know how I can prevent the camera from viewing outside the room?
You'll want to use clamp to keep the camera within the boundaries of your room.
It'll end up looking something like this.
x = clamp(x, CameraWidthHalf, room_width - CameraWidthHalf);
y = clamp(y, CameraHeightHalf, room_height - CameraHeightHalf);
I have an irregular shape map but i cannot change it so what should I do?
Love this, Shawn! Do you think there might be a way to implement some sort of following border to the character, so the camera only moves when they get a certain distance to the camera’s perimeter?
Think of it this way: You should only change the xTo and yTo coordinates if the follow object is far away. Extending the if statement to something simple like if(follow != noone && distance_to_object(follow) > radius) would do the trick (change radius in whatever the radius should be)
This is very handy!
how would I zoom in the camera?
Hey, Shaun. I'd like to thank you but I also got myself a problem, the camera does not work in a second room, can you help me with that?
If you want this to work with an instance. Use end step
I thought I did something wrong but could never figure out why, until I realized that when I typed the word "To" I put a P instead of an O. Lol
Hi Shaun, I am trying to use your code for a multiplayer game I'm working on. I want a camera for each individual player that focuses on their own assigned object. Also, I implemented the code shown in this video and ran the game, but when it started, the four objects (one for each player) were all in the bottom-right corner of the camera's view. Could you help me figure out what's going on and how this can be fixed? I'm very new to all this so please keep it simple if you can, thanks.
EDIT: I tried tinkering with the settings a little bit, and although I reset everything to what I originally had, the window is now incredibly small. What's going on? Thanks again.
masterpiece
tysm man
i did all of this but my camera actually moves diagonally when the player moves , cant find what i did wrong
edit: actually just rewrite all the code and it works
clap clap clap. if only i could comment audio
TYSM
How does one do an RTS cam
how do i make it so that doesnt go beyond the room border
I managed to find a pretty simple solution. On the step event, instead of having this:
xTo = follow.x;
yTo= follow. y;
It should be like this:
x_to = clamp(follow.x, 0+(cam_width/2), room_width-(cam_width/2));
y_to = clamp(follow.y, 0+(cam_height/2), room_height-(cam_height/2));
Clamp ensures that a value stays within a min and max value, in this case being the room's start and end. By adding (width/height /2) the camera object stops while letting the viewport contintue to the border.
(Sorry for flexing but I'm kinda proud for coming up with this myself.)
@@justanerd4462 it worked! thank youu
No problem.
I'll also just mention another thing I found which might be useful for anyone reading this.
When transitioning to another room the camera might briefly be outside the bounds and slide in. To prevent this, add this underneath the y_to/y_to:
EDIT: The code I previously suggested doesn't work on all sides, use this code instead.
x = clamp (x, (0 + cam_width/2), room_width - (cam_width/2));
y = clamp (y, (0 + cam_height/2), room_height - (cam_height/2));
This straight up tells the camera to stay within the bounds at all times.
gotcha!@@justanerd4462
Doesnt work for me, also my camera s not centered what can i do@@justanerd4462
why isnt mine working?
I put all this code in and it did nothing
for me neither
**SMASHES KEYBOARD** WHY DOESNT IT WORK, I PUT LETTER BY LETTER, AND IT DOESNT WORK WHY DOESNT IT WOOOOOORK
Define the problem before raging about it please, and show the code. Or your camera is just not in the room ;-;
@@Caveman835 cam in room and code is the exact same it just dont do anything for me
thanks my fryend😁
And my game looks twice as good in 20 minutes (im a bit slow)
THANKS!
Can you try make a video of adding the move sets of pizza tower for game maker I’m trying to make a game but I don’t know how to code
'fraid I don't know what that is :'(
Or can you make a video of how to put a unlimited dash move which uses the shift button but it for 2d platformer but if you want to know what pizza tower go to RUclips and search up pizza tower and one of the streams that was posted on RUclips it usually show him programming the game
This just straight up refuses to work, line for line, letter for letter, gamedev makes me so fucking tired of life sometimes.
Maybe you should follow it properly. It worked out fine for me I just need to adapt it to my irregular shaped map.
@@LostDeveloper871oh! Can I see how you adopted it to your map? My own is sliiighty funky and it follows just not closely enough
it no work
Sean Baldings done it again!
Really helpful, but one thing: When I did it, it was following my character a bit too far down to the right, making my character look in the top left. Any idea why this is????
Edit: Fixed it!!! For anybody else having this problem, set BOTH 0.5's to 1.0 and your set!
it doesnt work
Skill issue 😢🎉😮
camWidth = 640;
camHight = 480
follow = [Your Object];
xKamera = x
ykamera = y
if (follow != noone)
{
xKamera = follow.x;
yKamera = follow.y;
}
x += (xKamera - x)/25
y += (yKamera - y)/25
camera_set_view_pos(view_camera[0], x-(camWidth*0.5),y-(camHight*0.5));
Thank me later
thanks
😃
thanks thanks thanks thanks!!!