How to add a Game Camera that follows the player | Love2D Basics

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ม.ค. 2025

ความคิดเห็น • 95

  • @WNxExectechWarriorNation
    @WNxExectechWarriorNation 3 ปีที่แล้ว +49

    Awesome tutorial! My son and I are following your tutorials to create a game. Thank you Kyle, keep them coming!

    • @Challacade
      @Challacade  3 ปีที่แล้ว +8

      That's awesome!!

    • @MrInfarct
      @MrInfarct 2 ปีที่แล้ว +8

      It must be amazing that your father helps you making video games

    • @giuyo
      @giuyo ปีที่แล้ว +4

      W Father

    • @nito8066
      @nito8066 4 หลายเดือนก่อน

      how is it going

    • @willcubemakes
      @willcubemakes 4 หลายเดือนก่อน +1

      How's your game going? :)

  • @ismyname_jep1394
    @ismyname_jep1394 2 ปีที่แล้ว +7

    Very well explained. I feel like even if I use these in other languages I'd still be able to implement this feature because you taught the structure of the code logic so well.

  • @lamialua26
    @lamialua26 2 หลายเดือนก่อน +5

    In case anyone is interested, you can adjust how zoomed in/out the camera is by changing "cam.scale" in love.load()
    (e.g.
    cam.scale = cam.scale * 2)
    Then, in order for the borders to take that into account, you can also divide all the values by "cam.scale"
    (e.g.
    if cam.x < w/2/cam.scale then
    cam.x = w/2/cam.scale
    end)
    Not sure if it's the best way to do it but it seems to be working, hope this helps~

    • @HopelessGamer-rw5dz
      @HopelessGamer-rw5dz 12 วันที่ผ่านมา

      bro cam:zoom() exists

    • @lamialua26
      @lamialua26 5 วันที่ผ่านมา

      @@HopelessGamer-rw5dz Yeah sorry bro, I'm just a beginner, I do what I can
      Either way, that's good info

  • @justinlua4848
    @justinlua4848 2 ปีที่แล้ว +2

    I've been trying to do a lot of this the hard way without libraries. This series is so very useful!
    Also if you get any errors with loading the map, try this in Tileset: Edit > Preferences > General > Export Options > Embed tilesets = true

  • @casfewXD
    @casfewXD 3 ปีที่แล้ว +10

    For anyone that is having problems with the right and bottom corners.
    In the video, he uses an upscaled tileset for the map, but if u're using a normal one and upscaling it through the code, u'll have to multiply "mapW" and "mapH" by the same scale of the map that u multiplied it in the code.
    Hope that this will help someone :)

    • @ithalogomes6651
      @ithalogomes6651 2 ปีที่แล้ว

      AKDJAJDJWJFIWJD I LOVE YOU THANK YOU VERY MUCH

    • @mielland1621
      @mielland1621 ปีที่แล้ว

      I don't understand, can you please give me an example?

    • @juror12
      @juror12 ปีที่แล้ว

      Lol, thank you!

    • @ZigmaVR123
      @ZigmaVR123 6 หลายเดือนก่อน

      THANK YOU SO MUCH! After having to figure out how to resize individual layers (pain) I was so worried I had run into another issue. Your comment literally saved me!

    • @itsaducklin
      @itsaducklin 5 หลายเดือนก่อน

      @@ZigmaVR123 how did you manage that? I've been struggling with it as well

  • @Manny73211
    @Manny73211 5 หลายเดือนก่อน +2

    with sti and love, i thought "ok that's a funny coincedence", but HUMP??? NAH YOU GOTTA BE TROLLING

  • @comfixit
    @comfixit 2 ปีที่แล้ว

    Great tutorial, thanks for taking the time to make it.

  • @SandroSashz
    @SandroSashz หลายเดือนก่อน

    Muito obrigado!
    Seguindo suas instruções consegui tirar a barra preta embaixo da tela.

  • @stinkyseastar3560
    @stinkyseastar3560 ปีที่แล้ว

    Thank you so much for these tutorials, I find them very helpful!

  • @andrewmartin2341
    @andrewmartin2341 ปีที่แล้ว

    I was about to post that I had problem with my code and it wasn't checking the right border. Turns out I had cam.X instead of cam.x :D Great video man, you might consider posting a link to it in your udemy course somewhere, This is the missing camera piece to the videos you have there :)

  • @sethluca
    @sethluca 2 ปีที่แล้ว

    The sti thing that we imported last video doesnt work anymore for me when i try doing the layers at: 3:25. Do you know what the problem is?

    • @iamstill1451
      @iamstill1451 2 ปีที่แล้ว

      Did that work for u ?, I am having the same problem

    • @blackbart2682
      @blackbart2682 2 ปีที่แล้ว

      @@iamstill1451 ya it did was having same prob imported the same file with another name with lua ext

  • @musilicks
    @musilicks 27 วันที่ผ่านมา

    4:53 you could also just offset in the camera lookAt call itself: Cam:lookAt(Player.pos.x + Player.size.width / 2, Player.pos.y + Player.size.height / 2)

  • @naelpontes8444
    @naelpontes8444 2 ปีที่แล้ว

    3:28 if the layers are inside a folder, the drawing process is in any way different?

  • @patagain5161
    @patagain5161 2 ปีที่แล้ว

    merci pour se tuto, simple pratique et très bien expliqué pas à pas

  • @Simon-ik1kb
    @Simon-ik1kb ปีที่แล้ว

    super useful video, thank you.

  • @zenolth
    @zenolth 2 ปีที่แล้ว

    For the layer drawing i would just loop thru all the layers and draw them, makes it easier to add new ones ig

  • @berserkenjoyer-vh5yj
    @berserkenjoyer-vh5yj 2 ปีที่แล้ว +2

    hello i am newbie in Love2D and im trying to make camera fallow player as in guide, but when i write
    cam:attach, colon is no working i getting error how fix this?

  • @tlc1432
    @tlc1432 3 ปีที่แล้ว

    Thanks, this was really helpful

  • @danielalegriasallo5551
    @danielalegriasallo5551 3 ปีที่แล้ว

    Pretty useful tutorial, ty.

  • @lucaslindgren3237
    @lucaslindgren3237 3 ปีที่แล้ว

    This tutorial is great!

  • @Its_ME_Nightmare
    @Its_ME_Nightmare 2 ปีที่แล้ว

    I'm making a core game in love2D and need a way to make buttons that work and are on a control room area where you can press buttons on the panel this would be a huge help!

  • @gwrydd
    @gwrydd ปีที่แล้ว +2

    Just found these they’re very helpful. But please be careful you’re creating many globals which eventually is going to cause issues such as overwriting the same variable and performance issues since reading a global is slower than a local

  • @jdubz6396
    @jdubz6396 2 ปีที่แล้ว +2

    Hey! So my background is too small and originally I had it so it was scaled up 3x
    gameMap:draw(0, 0, 3)
    But when it draws individual layers it wouldn’t let me scale it. Do you know how to do this?

    • @ooooggll
      @ooooggll 2 ปีที่แล้ว

      I have the same problem
      Edit: I figured it out, for anyone else who has the problem as well.
      First I get the camera x and y coordinates with this line:
      local camx, camy = cam:position()
      then instead of gameMap:draw(0, 0, 3, 3) I do gameMap:draw(-camx/3, -camy/3, 3, 3)
      Basically just draw the map at the camera's position
      Edit 2: turns out this has some problems with stopping the camera at the border of the screen, and likely more problems later on. Also you can ignore the whole local camx, camy line, then replace camx with cam.x and camy with cam.y.

    • @berserkenjoyer-vh5yj
      @berserkenjoyer-vh5yj 2 ปีที่แล้ว

      @@ooooggll oh also do you know how to fix colon error in lua? when i writed exactly - cam:attach i had error by colon, but i dont understand why does it shows up please explain me if you can!

  • @donnab8477
    @donnab8477 ปีที่แล้ว

    How do you keep the little guy from walking off the edge into infinity? Is that in a later video?

  • @eon1411
    @eon1411 3 ปีที่แล้ว +1

    can you make a video on collisions? it would be a lot of help considering im trying to make a game that heavily relies on collision events AKA, making a video about making something happen when touching something else.

    • @Challacade
      @Challacade  3 ปีที่แล้ว +4

      Next tutorial is about adding collisions where the world is solid and the player stops - I'll work on adding in collision events as well!

  • @goladahagua273
    @goladahagua273 2 ปีที่แล้ว

    very good tutorial! but I wanted to know if there is a way to make the camera similar to the celeste camera.
    Would you help me with this?

  • @darkfrei2
    @darkfrei2 3 ปีที่แล้ว +1

    Can you explain how to make the same result, but without lib/camera?

    • @Challacade
      @Challacade  3 ปีที่แล้ว +2

      This is how I recommend doing cameras, so I don't plan on making any other videos about it. I highly suggest sticking with the library, or looking through the camera.lua file to see how it works behind the scenes

  • @idoblenderstuffs
    @idoblenderstuffs 2 ปีที่แล้ว +1

    werent getWidth and getHeight removed from love though?

    • @MaverickMeier
      @MaverickMeier 2 ปีที่แล้ว +1

      nooooo thats very important. why would they remove it?

  • @cbaptou
    @cbaptou ปีที่แล้ว

    How do i scale individual tile layers ?

  • @narikiskiran
    @narikiskiran 6 หลายเดือนก่อน

    I am having troubles, it says that the module camera.lua is not found. Any suggesting?

    • @itsaducklin
      @itsaducklin 5 หลายเดือนก่อน

      NOT camera.lua, just camera
      in require "camera.lua"
      it takes periods and uses them as like a forward slash /
      so, if you put camera.lua, it takes that as camera/lua, which doesn't exist.
      tldr;
      replace require "camera.lua" with require "camera"

    • @narikiskiran
      @narikiskiran 5 หลายเดือนก่อน

      @@itsaducklin thank you!

    • @itsaducklin
      @itsaducklin 5 หลายเดือนก่อน

      @@narikiskiran yw :>

  • @DarkkneZPanda
    @DarkkneZPanda 2 ปีที่แล้ว

    Was wondering if there was any type of zoom in function within the camera or love2d? My tiles are much smaller than 64 x 64 making my map bigger and tiles smaller. If anyone figures this out please let me know

  • @microistakeneverywhere
    @microistakeneverywhere 3 ปีที่แล้ว

    what about normal background? can i have the script for that?

  • @mrxoxtrew
    @mrxoxtrew 7 หลายเดือนก่อน

    after doing this i realized i still have the black background and my character is on it, can you help?

  • @wegwegedgweg5892
    @wegwegedgweg5892 2 ปีที่แล้ว

    I'm sorry, I was really stupid
    I'm really sorry, the video was great

  • @Ellohir
    @Ellohir 3 ปีที่แล้ว

    Cool! Wouldn't it be easier to center the camera by changing the lookAt to player.x+player.width/2 and player.y+player.height/2 ? I feel like drawing the sprite at the center could complicate a lot the collision detection later 🤔
    Anyway, great tutorial as always, bounding the camera makes it look very professional 👍🏻

    • @Challacade
      @Challacade  3 ปีที่แล้ว +2

      You are right about that other option, but I actually updated the draw offset in preparation for making collisions easier. The physics library we'll be using has the center set to the middle of the collider, so now it'll line up to be the same as the draw. And personally I like having the position match the center, it's easier for me to visualize

    • @Ellohir
      @Ellohir 3 ปีที่แล้ว

      @@Challacade awesome, thanks for your answer!

  • @manikandanu2317
    @manikandanu2317 2 ปีที่แล้ว

    Super... Thanks

  • @colwarsstudiobrickfilmandm8580
    @colwarsstudiobrickfilmandm8580 2 ปีที่แล้ว

    Would this work with an image instead of a tmx file?

    • @Challacade
      @Challacade  2 ปีที่แล้ว

      If you're using an image for the background rather than a tile map, it should still work. You'd just need to use the image width/height rather than the map's width/height.

  • @crimson3197
    @crimson3197 2 ปีที่แล้ว

    what about people who use background as a map

  • @jubayarrahman21
    @jubayarrahman21 ปีที่แล้ว

    i can't fegure out what's going but my cod dosen't work

  • @MaverickMeier
    @MaverickMeier 2 ปีที่แล้ว

    these really ugly lines appear quite frequently. is there a way to get rid of them?

  • @MrInfarct
    @MrInfarct 2 ปีที่แล้ว

    You're the best

  • @ejsuperstar
    @ejsuperstar ปีที่แล้ว

    I've been struggling for a few days now to get this right. My map is very small (20 by 20) and used small tiles (16 by 16) and just *cannot* seem to get the camera to stop at the right and bottom edges. I'm zooming in the camera using the camera zoom built into the camera. It works when I dont zoom in, but the moment I do everything goes wrong, I feel like i've tried everything I can. i know im over a year late to the party but I really need help and I dont know where else to look
    Edit: Less than 10 minutes after posting I figured out how to fix it.
    if cam.x > (mapW - w/2/camZoom)
    cam.x = (mapW - w/2/camZoom)
    end
    it was that easy, thats like, basically what I did for the top left, i cant believe it took me so long to figure out ... Either way uh, leaving this here incase anyone needs help too

  • @piquijeden
    @piquijeden 2 ปีที่แล้ว

    I have a problem. Player is not visible when i start the game

    • @anonymouslyanonymous09
      @anonymouslyanonymous09 ปีที่แล้ว

      Make sure that you are drawing the after the floor, but before anything you want to show infront of the player

  • @farjoos
    @farjoos 2 ปีที่แล้ว

    3:36

  • @PerlVeemo
    @PerlVeemo 3 ปีที่แล้ว +1

    Hump? 🤨

  • @growingmindset7
    @growingmindset7 2 ปีที่แล้ว

    this tutorial didnt worked... i am getting errors after errors and i did step by step like in the tutorial...

    • @MaverickMeier
      @MaverickMeier 2 ปีที่แล้ว

      colon not dot. pay very close attention to every line

  • @zurich_playzbg6165
    @zurich_playzbg6165 ปีที่แล้ว +2

    help "Error
    scripts/sti/init.lua:926: attempt to index local 'layer' (a nil value)
    Traceback
    [love "callbacks.lua"]:228: in function 'handler'
    scripts/sti/init.lua:926: in function 'drawLayer'
    main.lua:65: in function 'draw'
    [love "callbacks.lua"]:168: in function
    [C]: in function 'xpcall' "

  • @domivincze6525
    @domivincze6525 2 ปีที่แล้ว

    Hey! I have a problem that I cant figure out.
    the error says this:
    Error
    libraries/sti/init.lua:854: attempt to index local 'layer' (a nil value)
    Traceback
    [love "callbacks.lua"]:228: in function 'handler'
    libraries/sti/init.lua:854: in function 'drawLayer'
    main.lua:96: in function 'draw'
    [love "callbacks.lua"]:168: in function
    [C]: in function 'xpcall'
    I dont know what to do, please help Kyle!

  • @ndscartridges
    @ndscartridges หลายเดือนก่อน

    When I add cam.lookAt(player.x, player.y) the following error occurs (please note, i have followed the tutorial to the letter, even drawing layers instead of the map.)
    Error
    PGR/lib/camera.lua:72: attempt to index local 'self' (a number value)
    Traceback
    [love "callbacks.lua"]:228: in function 'handler'
    PGR/lib/camera.lua:72: in function 'lookAt'
    main.lua:66: in function 'update'
    [love "callbacks.lua"]:162: in function
    [C]: in function 'xpcall'

  • @MaverickMeier
    @MaverickMeier 2 ปีที่แล้ว

    umm I get an error when camera.lua tries to use self for lookAt

    • @MaverickMeier
      @MaverickMeier 2 ปีที่แล้ว

      nvm just make sure you use the colon and not the dot

  • @vircobb4077
    @vircobb4077 2 ปีที่แล้ว

    i kepp getting this error
    Error
    libraries/camera.lua:115: Maximum stack depth reached (more pushes than pops?)
    Traceback
    [love "callbacks.lua"]:228: in function 'handler'
    [C]: in function 'push'
    libraries/camera.lua:115: in function 'attach'
    main.lua:45: in function 'draw'
    [love "callbacks.lua"]:168: in function
    [C]: in function 'xpcall'

    • @vircobb4077
      @vircobb4077 2 ปีที่แล้ว

      never mind im just stupid

  • @povilasbuda6490
    @povilasbuda6490 9 หลายเดือนก่อน

    cam:lookAt(player.x, player.y)
    doesn't work for me, the player doesn't move when I press the movement keys
    Edit: I skipped the animation tutorial because I want to focus on that later

    • @povilasbuda6490
      @povilasbuda6490 7 หลายเดือนก่อน

      @@TheNew54321 Thanks!

  • @wegwegedgweg5892
    @wegwegedgweg5892 2 ปีที่แล้ว

    it gives me this error message
    Error
    libraries/camera.lua:109: attempt to index local 'self' (a nil value)
    Traceback
    [love "callbacks.lua"]:228: in function 'handler'
    libraries/camera.lua:109: in function 'attach'
    main.lua:69: in function 'draw'
    [love "callbacks.lua"]:168: in function
    [C]: in function 'xpcall'
    can someone help :(

    • @mingtongzhi
      @mingtongzhi 2 ปีที่แล้ว

      Make sure you have a : instead of a . on any function that needs to have a colon. I get this error a lot when I make this mistake and it's easy to overlook

  • @roujiteku2114
    @roujiteku2114 10 หลายเดือนก่อน

    just another guy with the same code error as the others. still no luck
    Error
    libraries/sti/init.lua:926: attempt to index local 'layer' (a nil value)
    Traceback
    [love "callbacks.lua"]:228: in function 'handler'
    libraries/sti/init.lua:926: in function 'drawLayer'
    main.lua:69: in function 'draw'
    [love "callbacks.lua"]:168: in function
    [C]: in function 'xpcall'

    • @roujiteku2114
      @roujiteku2114 10 หลายเดือนก่อน

      okay i fixed it, i apparently renamed one of my layers in Tiled, which i did save but i apparently didn't exporting the new save. that worked, im stupid

    • @Walibear2
      @Walibear2 5 หลายเดือนก่อน

      Thanks bro ur a legend