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

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ • 119

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

  • @burningangel00
    @burningangel00 2 роки тому +14

    That short format is pure gold!

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

  • @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 4 місяці тому +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 Місяць тому +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!

  • @LunarcomplexMain
    @LunarcomplexMain 2 роки тому +25

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

  • @cleancrevice2100
    @cleancrevice2100 Рік тому +3

    Thanks!

  • @lukabajic3122
    @lukabajic3122 7 місяців тому +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!

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

  • @lando1296
    @lando1296 7 місяців тому +4

    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.

  • @misterr3083
    @misterr3083 2 роки тому +2

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

  • @fattcoke4705
    @fattcoke4705 Рік тому +1

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

  • @Nintendogs08
    @Nintendogs08 Рік тому +6

    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!

  • @trovideovault9657
    @trovideovault9657 Місяць тому

    wow this worked thanks~ looks awesome now

  • @77.corrupter.77
    @77.corrupter.77 Рік тому +6

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

    • @mfyz_0
      @mfyz_0 Рік тому +1

      did u put the camera object into the room?

    • @77.corrupter.77
      @77.corrupter.77 Рік тому +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: ua-cam.com/video/YbppozAV1Q8/v-deo.html

  • @yeshelp9497
    @yeshelp9497 2 роки тому +4

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

  • @-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 6 місяців тому

      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 6 місяців тому

      @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 6 місяців тому

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

    • @-Russian_Knight
      @-Russian_Knight 6 місяців тому

      @gianstb Thank you ❤️

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

  • @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 11 місяців тому

      This was very helpful, ty.@@jackkraus6948

  • @Raadidas
    @Raadidas 7 місяців тому +1

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

  • @shadowshifter725
    @shadowshifter725 10 місяців тому

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

  • @DocMonitor
    @DocMonitor Рік тому +1

    can u also make it using GM visual?

  • @SantaAzulCTZN
    @SantaAzulCTZN 13 днів тому

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

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

  • @gemufox
    @gemufox Рік тому

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

  • @PYROmansion
    @PYROmansion Місяць тому

    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.

  • @bgdog31
    @bgdog31 11 місяців тому

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

  • @projectRaMan
    @projectRaMan 10 місяців тому

    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?

  • @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 6 місяців тому +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

  • @theManWithoutFace
    @theManWithoutFace 11 місяців тому

    oh damn thanks a lot bro

  • @genoTheOnly
    @genoTheOnly Місяць тому

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

  • @nicdepp
    @nicdepp 11 місяців тому

    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 6 місяців тому

      same, did you find a fix?

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

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

  • @angelthebox
    @angelthebox 4 місяці тому

    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 4 місяці тому +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 3 місяці тому

      @@tym4971 thanks! it's working

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

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

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

  • @blup9997
    @blup9997 5 місяців тому

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

  • @Thegoldenbard
    @Thegoldenbard 9 місяців тому

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

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

  • @LostDeveloper871
    @LostDeveloper871 Рік тому

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

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

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

  • @daisukicliff9119
    @daisukicliff9119 2 місяці тому

    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

  • @thirdworldrider6991
    @thirdworldrider6991 11 місяців тому

    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.

  • @agudar
    @agudar 2 роки тому

    This is very handy!

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

  • @BUFFGOG
    @BUFFGOG 11 місяців тому

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

  • @RacTeamGames
    @RacTeamGames Рік тому +2

    mine is not on the center

  • @Pienotpi
    @Pienotpi 8 місяців тому

    how would I zoom in the camera?

  • @Some-One4
    @Some-One4 Рік тому

    tysm man

  • @5-Volts
    @5-Volts Місяць тому

    clap clap clap. if only i could comment audio
    TYSM

  • @thesynthpub6511
    @thesynthpub6511 Рік тому

    How does one do an RTS cam

  • @gdog4202
    @gdog4202 Рік тому

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

  • @vaguener
    @vaguener 11 місяців тому

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

  • @ivanrehetskyi4830
    @ivanrehetskyi4830 Рік тому

    masterpiece

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

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

  • @EleventhStar-q4r
    @EleventhStar-q4r 3 місяці тому

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

    • @Caveman835
      @Caveman835 3 місяці тому

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

    • @ananasdragen5782
      @ananasdragen5782 Місяць тому

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

  • @maxhans7783
    @maxhans7783 Рік тому

    why isnt mine working?

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

  • @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 10 місяців тому

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

  • @docshrekboticus
    @docshrekboticus Рік тому +3

    I put all this code in and it did nothing

  • @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 9 місяців тому

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

  • @CalvinLol777
    @CalvinLol777 9 місяців тому

    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!

  • @gamermasta525
    @gamermasta525 2 роки тому

    Sean Baldings done it again!

  • @playsangel558
    @playsangel558 Рік тому

    it no work

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