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

Поделиться
HTML-код
  • Опубликовано: 18 янв 2025

Комментарии • 127

  • @SaraSpalding
    @SaraSpalding  2 года назад +40

    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 Год назад +27

    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 2 года назад +16

    That short format is pure gold!

  • @LunarcomplexMain
    @LunarcomplexMain 2 года назад +25

    Love these short and to the point knowledge bursts of pretty useful information, thanks!

  • @cederickforsberg5840
    @cederickforsberg5840 Год назад +29

    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...

    • @bendoverman68
      @bendoverman68 5 месяцев назад +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

    • @kaspinator5265
      @kaspinator5265 2 месяца назад +1

      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!

  • @lukabajic3122
    @lukabajic3122 8 месяцев назад +1

    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!

  • @cleancrevice2100
    @cleancrevice2100 Год назад +5

    Thanks!

  • @nomhiz
    @nomhiz 2 года назад +6

    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!

  • @JonnyGetsLost
    @JonnyGetsLost 16 дней назад

    fantastic tutorial, extremely easy to follow and straight to the point. Thank you ever so much for making this

  • @misterr3083
    @misterr3083 2 года назад +3

    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!!!

  • @lando1296
    @lando1296 8 месяцев назад +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 7 месяцев назад +1

      Thanks!

    • @grigla2235
      @grigla2235 13 дней назад +1

      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!!

    • @lando1296
      @lando1296 13 дней назад

      @@grigla2235 Can do. Glad I was able to help.

  • @Goofy_Guys_VR
    @Goofy_Guys_VR 6 дней назад

    Holy guacamole! You've earned a subscriber!

  • @fattcoke4705
    @fattcoke4705 Год назад +1

    Thanks man! This worked perfectly first try. Super quick way to set up an epic camera. 10/10

  • @Nintendogs08
    @Nintendogs08 Год назад +8

    This is a great video! It helped a lot!
    However, how do I make it stop when it intersects with the Room Boundary?

  • @theempireofstud
    @theempireofstud 2 года назад +2

    You have helped me so much in my career of coding so thanks!

  • @77.corrupter.77
    @77.corrupter.77 2 года назад +7

    idk why but the camera wont move to the player, i tried everything but it wont work.

    • @mfyz_0
      @mfyz_0 2 года назад +1

      did u put the camera object into the room?

    • @77.corrupter.77
      @77.corrupter.77 2 года назад +1

      @@mfyz_0 yes

    • @angharadedwards4835
      @angharadedwards4835 Год назад +2

      Did you even find a fix for this? I have the same problem. Quadruple checked the code, cameras in room etc.

    • @JamieWisecarver
      @JamieWisecarver Год назад +2

      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

  • @trovideovault9657
    @trovideovault9657 2 месяца назад

    wow this worked thanks~ looks awesome now

  • @-Russian_Knight
    @-Russian_Knight Год назад +2

    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 7 месяцев назад

      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 7 месяцев назад

      @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 7 месяцев назад

      @@-Russian_Knight It's ok man, and don't worry, your english is very good

    • @-Russian_Knight
      @-Russian_Knight 7 месяцев назад

      @gianstb Thank you ❤️

  • @digitalswordplay
    @digitalswordplay Год назад +2

    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 Год назад +1

      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 Год назад +1

      ​@@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 Год назад +2

      ​@@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_ Год назад

      @@jackkraus6948 where would you implement this code

    • @askvigg8355
      @askvigg8355 Год назад

      This was very helpful, ty.@@jackkraus6948

  • @BUFFGOG
    @BUFFGOG Год назад +1

    how do I stop the camera at the edge of the room

  • @SantaAzulCTZN
    @SantaAzulCTZN Месяц назад

    What if you want the camera to focus on more than one object like, the two players in a fighting game?

  • @NickyLunaLove
    @NickyLunaLove 7 дней назад

    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 😅

  • @Raadidas
    @Raadidas 8 месяцев назад +1

    I love the video but none of the code works in the GameMaker studio 1.4 for some odd reason

  • @yeshelp9497
    @yeshelp9497 2 года назад +4

    This is wonderful! How would you go about zooming in and out the camera though?

  • @genoautista
    @genoautista 2 месяца назад

    how can i limit the camera movement so it doesnt gets out of the room?

  • @DocMonitor
    @DocMonitor Год назад +1

    can u also make it using GM visual?

  • @PYROmansion
    @PYROmansion 2 месяца назад

    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.

  • @thirdworldrider6991
    @thirdworldrider6991 Год назад

    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.

  • @shadowshifter725
    @shadowshifter725 11 месяцев назад

    Great tutuorial! But how do you make the camera stop when it reaches the edge of the room?

  • @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?

  • @zorex324
    @zorex324 2 года назад +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?

    • @SaraSpalding
      @SaraSpalding  2 года назад +4

      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 2 года назад

      @@SaraSpalding Okay thanks for the quick response!

  • @daisukicliff9119
    @daisukicliff9119 3 месяца назад

    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

  • @blup9997
    @blup9997 6 месяцев назад

    everything becomes streched when i finished coding the camera? this also happened when i followed a diferent tutorial, please help

  • @nicdepp
    @nicdepp Год назад

    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 7 месяцев назад

      same, did you find a fix?

  • @eagle9717
    @eagle9717 Год назад

    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?

  • @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).

  • @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

  • @gemufox
    @gemufox Год назад

    nice video, thanks for this good content, finally i found the solution of my problem,
    again, thanks you

  • @angelthebox
    @angelthebox 5 месяцев назад

    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 5 месяцев назад +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 4 месяца назад

      @@tym4971 thanks! it's working

  • @Thegoldenbard
    @Thegoldenbard 10 месяцев назад

    So! I got it to work, but it doesn’t zoom in as close as I need it to, what do I do?

  • @vaguener
    @vaguener Год назад

    Correction, not ANY GameMaker, camera-set is only for Gamemaker 2.

  • @RacTeamGames
    @RacTeamGames Год назад +2

    mine is not on the center

  • @gluehole4036
    @gluehole4036 Год назад +1

    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 7 месяцев назад +1

      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

  • @projectRaMan
    @projectRaMan 11 месяцев назад

    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?

  • @bgdog31
    @bgdog31 Год назад

    I have a screen shake sprite, and whenever the screen shakes, the camera turns off. how do I fix this?

  • @theManWithoutFace
    @theManWithoutFace Год назад

    oh damn thanks a lot bro

  • @FernandoLopez-uf6ch
    @FernandoLopez-uf6ch 2 года назад +3

    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 2 года назад +2

      What I'm trying to say is that on camera it shows me parts of the room that are empty

    • @SaraSpalding
      @SaraSpalding  2 года назад +7

      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.

  • @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."

  • @lebobshark
    @lebobshark Год назад +1

    anyone know how I can prevent the camera from viewing outside the room?

    • @OminousGiggle
      @OminousGiggle Год назад +1

      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);

  • @LostDeveloper871
    @LostDeveloper871 Год назад

    I have an irregular shape map but i cannot change it so what should I do?

  • @yes3714
    @yes3714 Год назад +1

    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)

  • @agudar
    @agudar 2 года назад

    This is very handy!

  • @Pienotpi
    @Pienotpi 9 месяцев назад

    how would I zoom in the camera?

  • @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?

  • @gdog4202
    @gdog4202 Год назад

    If you want this to work with an instance. Use end step

  • @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

  • @evanperrine4012
    @evanperrine4012 2 года назад

    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.

  • @ivanrehetskyi4830
    @ivanrehetskyi4830 Год назад

    masterpiece

  • @Some-One4
    @Some-One4 2 года назад

    tysm man

  • @franxd323
    @franxd323 2 года назад

    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

  • @5-Volts
    @5-Volts 2 месяца назад

    clap clap clap. if only i could comment audio
    TYSM

  • @thesynthpub6511
    @thesynthpub6511 Год назад

    How does one do an RTS cam

  • @itstakku6178
    @itstakku6178 Год назад +1

    how do i make it so that doesnt go beyond the room border

    • @justanerd4462
      @justanerd4462 Год назад +3

      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 Год назад +1

      @@justanerd4462 it worked! thank youu

    • @justanerd4462
      @justanerd4462 Год назад +1

      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 Год назад +1

      gotcha!@@justanerd4462

    • @1vskill423
      @1vskill423 11 месяцев назад

      Doesnt work for me, also my camera s not centered what can i do@@justanerd4462

  • @maxhans7783
    @maxhans7783 Год назад

    why isnt mine working?

  • @docshrekboticus
    @docshrekboticus Год назад +3

    I put all this code in and it did nothing

  • @EleventhStar-q4r
    @EleventhStar-q4r 4 месяца назад

    **SMASHES KEYBOARD** WHY DOESNT IT WORK, I PUT LETTER BY LETTER, AND IT DOESNT WORK WHY DOESNT IT WOOOOOORK

    • @Caveman835
      @Caveman835 4 месяца назад

      Define the problem before raging about it please, and show the code. Or your camera is just not in the room ;-;

    • @ananasdragen5782
      @ananasdragen5782 2 месяца назад

      @@Caveman835 cam in room and code is the exact same it just dont do anything for me

  • @u32154
    @u32154 Год назад

    thanks my fryend😁

  • @cleancrevice2100
    @cleancrevice2100 Год назад

    And my game looks twice as good in 20 minutes (im a bit slow)
    THANKS!

  • @tailsrgt5705
    @tailsrgt5705 2 года назад

    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  2 года назад

      'fraid I don't know what that is :'(

    • @tailsrgt5705
      @tailsrgt5705 2 года назад

      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

  • @joshuawatson9077
    @joshuawatson9077 Год назад +3

    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 10 месяцев назад

      @@LostDeveloper871oh! Can I see how you adopted it to your map? My own is sliiighty funky and it follows just not closely enough

  • @playsangel558
    @playsangel558 Год назад

    it no work

  • @gamermasta525
    @gamermasta525 2 года назад

    Sean Baldings done it again!

  • @CalvinLol777
    @CalvinLol777 10 месяцев назад

    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!

  • @yt_jonathan6394
    @yt_jonathan6394 Год назад +1

    it doesnt work

  • @steinklotz69
    @steinklotz69 Год назад +11

    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