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!
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.
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!!!
I had the same problem, I just went ahead and followed an older version of his tutorial for his platformer series: ua-cam.com/video/YbppozAV1Q8/v-deo.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.
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.
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);
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.
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.
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?
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
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
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?
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);
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).
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)
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);
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
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.
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
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 UA-cam and search up pizza tower and one of the streams that was posted on UA-cam it usually show him programming the game
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.
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.
That short format is pure gold!
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
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!
Love these short and to the point knowledge bursts of pretty useful information, thanks!
Thanks!
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!
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!
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!
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!
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!
wow this worked thanks~ looks awesome now
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: ua-cam.com/video/YbppozAV1Q8/v-deo.html
This is wonderful! How would you go about zooming in and out the camera though?
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 ❤️
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.
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
I love the video but none of the code works in the GameMaker studio 1.4 for some odd reason
Great tutuorial! But how do you make the camera stop when it reaches the edge of the room?
can u also make it using GM visual?
What if you want the camera to focus on more than one object like, the two players in a fighting game?
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!
nice video, thanks for this good content, finally i found the solution of my problem,
again, thanks you
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.
I have a screen shake sprite, and whenever the screen shakes, the camera turns off. how do I fix this?
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?
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
oh damn thanks a lot bro
how can i limit the camera movement so it doesnt gets out of the room?
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?
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?
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?
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
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).
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)
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);
everything becomes streched when i finished coding the camera? this also happened when i followed a diferent tutorial, please help
So! I got it to work, but it doesn’t zoom in as close as I need it to, what do I do?
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."
I have an irregular shape map but i cannot change it so what should I do?
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
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?
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
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.
This is very handy!
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.
how do I stop the camera at the edge of the room
mine is not on the center
same
same
how would I zoom in the camera?
tysm man
clap clap clap. if only i could comment audio
TYSM
How does one do an RTS cam
If you want this to work with an instance. Use end step
Correction, not ANY GameMaker, camera-set is only for Gamemaker 2.
masterpiece
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
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
**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!
why isnt mine working?
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 UA-cam and search up pizza tower and one of the streams that was posted on UA-cam it usually show him programming the game
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
I put all this code in and it did nothing
for me neither
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
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!
Sean Baldings done it again!
it no work
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!!!