How to make a Camera for any GameMaker game in 4 minutes

  Рет қаралды 50,847

Sara Spalding

Sara Spalding

Күн бұрын

Make a flexible 2d camera that follows an arbitrary target with some nice, smooth easing in a tiny amount of GameMaker code. Use it as a foundation for more complex camera rules that suit your particular game design.
▶ Support my work: / shaunjs
▶ Source code: / new-camera-74441424
▶ Camera design theory: www.gamedevelo...
If you liked this short and easy tutorial for GameMaker please consider supporting my work so that I can make more of them! I cover all things GameMaker (formerly GameMaker Studio) and I'm also developing a game called PokeyPoke that you can check out here:
▶ Wishlist my game PokeyPoke: store.steampow...

Пікірлер: 111
@SaraSpalding
@SaraSpalding Жыл бұрын
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.
@EthicalAllele
@EthicalAllele 10 ай бұрын
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
@burningangel00
@burningangel00 Жыл бұрын
That short format is pure gold!
@LunarcomplexMain
@LunarcomplexMain Жыл бұрын
Love these short and to the point knowledge bursts of pretty useful information, thanks!
@lukabajic3122
@lukabajic3122 5 ай бұрын
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!
@nomhiz
@nomhiz Жыл бұрын
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!
@misterr3083
@misterr3083 Жыл бұрын
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!!!
@SaraSpalding
@SaraSpalding Жыл бұрын
Glad you enjoyed it!
@cederickforsberg5840
@cederickforsberg5840 Жыл бұрын
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...
@abunchofbumsinc
@abunchofbumsinc 2 ай бұрын
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
@fattcoke4705
@fattcoke4705 Жыл бұрын
Thanks man! This worked perfectly first try. Super quick way to set up an epic camera. 10/10
@theempireofstud
@theempireofstud Жыл бұрын
You have helped me so much in my career of coding so thanks!
@Nintendogs08
@Nintendogs08 11 ай бұрын
This is a great video! It helped a lot! However, how do I make it stop when it intersects with the Room Boundary?
@yeshelp9497
@yeshelp9497 Жыл бұрын
This is wonderful! How would you go about zooming in and out the camera though?
@cleancrevice2100
@cleancrevice2100 Жыл бұрын
Thanks!
@Poetax
@Poetax Жыл бұрын
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?
@lando1296
@lando1296 5 ай бұрын
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.
@pyroyt8912
@pyroyt8912 4 ай бұрын
Thanks!
@yes3714
@yes3714 Жыл бұрын
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?
@MazeyVidya
@MazeyVidya Жыл бұрын
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)
@theManWithoutFace
@theManWithoutFace 9 ай бұрын
oh damn thanks a lot bro
@gemufox
@gemufox Жыл бұрын
nice video, thanks for this good content, finally i found the solution of my problem, again, thanks you
@agudar
@agudar Жыл бұрын
This is very handy!
@Some-One4
@Some-One4 Жыл бұрын
tysm man
@shadowshifter725
@shadowshifter725 8 ай бұрын
Great tutuorial! But how do you make the camera stop when it reaches the edge of the room?
@gnysek
@gnysek Жыл бұрын
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).
@thirdworldrider6991
@thirdworldrider6991 9 ай бұрын
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.
@FernandoLopez-uf6ch
@FernandoLopez-uf6ch Жыл бұрын
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
@FernandoLopez-uf6ch
@FernandoLopez-uf6ch Жыл бұрын
What I'm trying to say is that on camera it shows me parts of the room that are empty
@SaraSpalding
@SaraSpalding Жыл бұрын
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.
@daisukicliff9119
@daisukicliff9119 5 күн бұрын
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
@digitalswordplay
@digitalswordplay 11 ай бұрын
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 ?
@jackkraus6948
@jackkraus6948 11 ай бұрын
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
@echopaff
@echopaff 10 ай бұрын
​@@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));
@jackkraus6948
@jackkraus6948 10 ай бұрын
​@@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);
@Vertech_
@Vertech_ 10 ай бұрын
@@jackkraus6948 where would you implement this code
@askvigg8355
@askvigg8355 9 ай бұрын
This was very helpful, ty.@@jackkraus6948
@ivanrehetskyi4830
@ivanrehetskyi4830 11 ай бұрын
masterpiece
@-Russian_Knight
@-Russian_Knight Жыл бұрын
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...
@gianstb
@gianstb 4 ай бұрын
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
@-Russian_Knight
@-Russian_Knight 4 ай бұрын
@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.
@gianstb
@gianstb 4 ай бұрын
@@-Russian_Knight It's ok man, and don't worry, your english is very good
@-Russian_Knight
@-Russian_Knight 4 ай бұрын
@gianstb Thank you ❤️
@77.corrupter.77
@77.corrupter.77 Жыл бұрын
idk why but the camera wont move to the player, i tried everything but it wont work.
@mfyz_0
@mfyz_0 Жыл бұрын
did u put the camera object into the room?
@77.corrupter.77
@77.corrupter.77 Жыл бұрын
@@mfyz_0 yes
@angharadedwards4835
@angharadedwards4835 Жыл бұрын
Did you even find a fix for this? I have the same problem. Quadruple checked the code, cameras in room etc.
@JamieWisecarver
@JamieWisecarver Жыл бұрын
I had the same problem, I just went ahead and followed an older version of his tutorial for his platformer series: kzbin.info/www/bejne/j5PToaKwdrtkh5o
@Raadidas
@Raadidas 5 ай бұрын
I love the video but none of the code works in the GameMaker studio 1.4 for some odd reason
@vaguener
@vaguener 9 ай бұрын
Correction, not ANY GameMaker, camera-set is only for Gamemaker 2.
@angelthebox
@angelthebox 2 ай бұрын
The camera follow works perfectly, but is there a way to make the camera object not go out of the room's bounds?
@tym4971
@tym4971 2 ай бұрын
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);
@angelthebox
@angelthebox Ай бұрын
@@tym4971 thanks! it's working
@zorex324
@zorex324 Жыл бұрын
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?
@SaraSpalding
@SaraSpalding Жыл бұрын
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.
@zorex324
@zorex324 Жыл бұрын
@@SaraSpalding Okay thanks for the quick response!
@u32154
@u32154 Жыл бұрын
thanks my fryend😁
@cleancrevice2100
@cleancrevice2100 Жыл бұрын
And my game looks twice as good in 20 minutes (im a bit slow) THANKS!
@superplush9987
@superplush9987 Жыл бұрын
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
@blup9997
@blup9997 3 ай бұрын
everything becomes streched when i finished coding the camera? this also happened when i followed a diferent tutorial, please help
@eagle9717
@eagle9717 10 ай бұрын
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?
@DocMonitor
@DocMonitor 11 ай бұрын
can u also make it using GM visual?
@evanperrine4012
@evanperrine4012 Жыл бұрын
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.
@thehbr7133
@thehbr7133 Жыл бұрын
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?
@nicdepp
@nicdepp 9 ай бұрын
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
@pyroyt8912
@pyroyt8912 4 ай бұрын
same, did you find a fix?
@franxd323
@franxd323 Жыл бұрын
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
@justphoenyx05
@justphoenyx05 Жыл бұрын
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
@gdog4202
@gdog4202 Жыл бұрын
If you want this to work with an instance. Use end step
@Thegoldenbard
@Thegoldenbard 7 ай бұрын
So! I got it to work, but it doesn’t zoom in as close as I need it to, what do I do?
@projectRaMan
@projectRaMan 8 ай бұрын
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?
@EleventhStar-q4r
@EleventhStar-q4r Ай бұрын
**SMASHES KEYBOARD** WHY DOESNT IT WORK, I PUT LETTER BY LETTER, AND IT DOESNT WORK WHY DOESNT IT WOOOOOORK
@Caveman835
@Caveman835 Ай бұрын
Define the problem before raging about it please, and show the code. Or your camera is just not in the room ;-;
@gluehole4036
@gluehole4036 Жыл бұрын
Using this camera code in a 2d platformer, how would I stop the camera from exiting the boundaries of the room?
@SammySimmo-up7tm
@SammySimmo-up7tm 4 ай бұрын
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
@Cartoonicus
@Cartoonicus Жыл бұрын
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."
@bgdog31
@bgdog31 9 ай бұрын
I have a screen shake sprite, and whenever the screen shakes, the camera turns off. how do I fix this?
@LostDeveloper871
@LostDeveloper871 Жыл бұрын
I have an irregular shape map but i cannot change it so what should I do?
@BUFFGOG
@BUFFGOG 9 ай бұрын
how do I stop the camera at the edge of the room
@Pienotpi
@Pienotpi 6 ай бұрын
how would I zoom in the camera?
@lebobshark
@lebobshark Жыл бұрын
anyone know how I can prevent the camera from viewing outside the room?
@OminousGiggle
@OminousGiggle Жыл бұрын
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);
@RacTeamGames
@RacTeamGames Жыл бұрын
mine is not on the center
@Gontrilla
@Gontrilla Жыл бұрын
same
@pyroyt8912
@pyroyt8912 4 ай бұрын
same
@thesynthpub6511
@thesynthpub6511 Жыл бұрын
How does one do an RTS cam
@gamermasta525
@gamermasta525 Жыл бұрын
Sean Baldings done it again!
@maxhans7783
@maxhans7783 10 ай бұрын
why isnt mine working?
@docshrekboticus
@docshrekboticus Жыл бұрын
I put all this code in and it did nothing
@cederickforsberg5840
@cederickforsberg5840 Жыл бұрын
for me neither
@CalvinLol777
@CalvinLol777 7 ай бұрын
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!
@joshuawatson9077
@joshuawatson9077 Жыл бұрын
This just straight up refuses to work, line for line, letter for letter, gamedev makes me so fucking tired of life sometimes.
@LostDeveloper871
@LostDeveloper871 Жыл бұрын
Maybe you should follow it properly. It worked out fine for me I just need to adapt it to my irregular shaped map.
@Thegoldenbard
@Thegoldenbard 7 ай бұрын
@@LostDeveloper871oh! Can I see how you adopted it to your map? My own is sliiighty funky and it follows just not closely enough
@tailsrgt5705
@tailsrgt5705 Жыл бұрын
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
@SaraSpalding
@SaraSpalding Жыл бұрын
'fraid I don't know what that is :'(
@tailsrgt5705
@tailsrgt5705 Жыл бұрын
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 KZbin and search up pizza tower and one of the streams that was posted on KZbin it usually show him programming the game
@playsangel558
@playsangel558 11 ай бұрын
it no work
@itstakku6178
@itstakku6178 Жыл бұрын
how do i make it so that doesnt go beyond the room border
@justanerd4462
@justanerd4462 Жыл бұрын
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.)
@itstakku6178
@itstakku6178 Жыл бұрын
@@justanerd4462 it worked! thank youu
@justanerd4462
@justanerd4462 Жыл бұрын
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.
@itstakku6178
@itstakku6178 Жыл бұрын
gotcha!@@justanerd4462
@1vskill423
@1vskill423 8 ай бұрын
Doesnt work for me, also my camera s not centered what can i do@@justanerd4462
@yt_jonathan6394
@yt_jonathan6394 Жыл бұрын
it doesnt work
@randompandemonium4823
@randompandemonium4823 Жыл бұрын
Skill issue 😢🎉😮
@steinklotz69
@steinklotz69 Жыл бұрын
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
@Under_Suspicion
@Under_Suspicion Жыл бұрын
thanks 😃
@theManWithoutFace
@theManWithoutFace 9 ай бұрын
thanks thanks thanks thanks!!!
Game Maker Studio 2 - The Basics of Cameras
11:47
Let's Learn This Together
Рет қаралды 3,5 М.
How I Turned a Lolipop Into A New One 🤯🍭
00:19
Wian
Рет қаралды 11 МЛН
НИКИТА ПОДСТАВИЛ ДЖОНИ 😡
01:00
HOOOTDOGS
Рет қаралды 2,1 МЛН
World‘s Strongest Man VS Apple
01:00
Browney
Рет қаралды 68 МЛН
Object States | Game Maker Studio 2
22:21
FriendlyCosmonaut
Рет қаралды 137 М.
How to Code (almost) Any Feature
9:48
DaFluffyPotato
Рет қаралды 689 М.
RESOLUTION guide for game devs & GameMaker
8:50
Sara Spalding
Рет қаралды 64 М.
Bounds Camera In GameMaker Studio 2.3
10:02
GameMaker Casts
Рет қаралды 1,7 М.
GameMaker Studio 2: Melee Attacks Tutorial
31:48
Sara Spalding
Рет қаралды 196 М.
I Made the Same Game in 8 Engines
12:34
Emeral
Рет қаралды 4,1 МЛН
How to Make a Good 2D Camera
11:38
Game Maker's Toolkit
Рет қаралды 410 М.
Why you Draw Bad Assets || 2D Game Art
13:00
Nonsensical 2D
Рет қаралды 75 М.
Smooth Camera Tutorial + Pan/Zoom [GameMaker Studio 2]
7:15
GameMakerStation - Matharoo
Рет қаралды 27 М.
How I Turned a Lolipop Into A New One 🤯🍭
00:19
Wian
Рет қаралды 11 МЛН