GameMaker Studio 2: Camera Follow Player/Object
Вставка
- Опубліковано 26 вер 2018
- This is a quick video on how to make viewports follow an object. The actual coding required is 3 lines.... so I spend a while explaining things!
If you want too know how to ALSO clamp the camera based on room size, check out this video: • GameMaker Studio 2: Ca...
[--- SUPPORT ME / SOCIAL MEDIA ---]
Discord: / discord
Patreon: / gamemakerrob
Itch: gamemaker-rob.itch.io/
Twitter: / randomdude_sdu
======
Credits
======
Music by Avgvst.
Licensed CC-BY
Monster Sprite by David Gervais
creativecommons.org/licenses/...
hello, i'm from Brazil and i'm writing this in the translator because i can't speak english, but i'd like to thank you for this code you made, saved mine and motivated me to continue programming :)
That's great to hear man - keep it up!
Sou br tbm e estou iniciando na programação, tutorial mt show!!!
tbm sou br e é muito bom o tutorial
I think this is the best video on camera basics out there, thanks for keeping it simple.
Glad you like it ;)
Thank you so much for this video! I'm in a game development class and my teacher never taught us how to make the camera actually move with the player, so this saved my final project from looking really weird.
you're welcome!
Thank you! This is exactly what I needed to manipulate my cameras in cutscene sequences, based on an invisible object on the timeline (track panel). Great job.
I did not know you could have collapsible code. This video was worth it just for that. Ace.
YOU SOLVED 1 WEEK STRUGGLING WITH JITTERING WHEN FIXING DIAGONAL MOVE, TYSM
Thank you so much for making this! I was going crazy trying to figure out how to fix the shaking from the viewports. Edit I had no idea it was the viewport, everything runs perfect now.
You're welcome!
I've been trying to figure this out for literal hours, thank you
THANK YOU FINALLY A STRAIGHTFORWARD TUT
THANK YOU SO MUCH BRO YOU SO HELPED ME FOR MY GAME
Fragmentori YOURE WELCOME!!
Great and simple code, keep up the good work!
Want to clamp the camera to rooms of different sizes as well? (stop the camera going outside the boundaries of the room, regardless of room size) ua-cam.com/video/V8EQFq7GZgo/v-deo.html
ur intro is sick dude :d but btw great tutorial, i subbed!
thanks man :)
Had to dig this one out to do a little RPG math game for my boy today. So happy.
How old is he?
@@GameMakerRob He is 9. I thought over the summer I would make a simple RPG. For practicing his multiplications he could go around looking for the correct answers shooting up stuff. The regular math games are not that exciting. Have to blow stuff and have some fun and learn too....haha.. thanks
Easy to code and really working
Love it!
Thank you so much dude, we brazilians have a expression for when someone really fucking help us a lot, is is "quebrou um galho e tanto"
Really thanks bro
Tou are amazing
Clayton Scarmussin you're welcome!
Hehehe um br por aqui ^^
yaay, br's criando jogos ^-^
Eu conheço essa expressão de algum lugar... seria você um brazilian?
@@ofaca1429 are you brazillian mermão? Very loco ver brazillian in every place
thank you! worked perfectly!
Bro i leanred so much!
this guy deserves a like i tried many tutorials and it did not work but this is the only one that works
thank you so much!! the clearest video about the camera I can find on youtube. Cheers mate!
Kevin Long you're welcome! If you also want to know how to stop the camera moving outside the room there's a follow-up video in the description.
@@GameMakerRob Sounds great! I'll be sure to check it out. Also I'm wondering do you happen to have a tutorial about how to animate the character in a 2D game like the one you show in the video? I'm trying to make an exactly same game. Thanks!
Kevin Long lol well it just so happens I made a 25 episode series o how to make this kind of game and the video on how to make the characters change sprites is pretty early on. Check out the "Player Movement" video in the RPG starter kit series
@@GameMakerRob That's brilliant! I'll go check them out. Appreciate it!
YOU ARE A LEGEND. I swear this video will help anyone despite when they watch it.
haha
Worked great! Thanks!
Thank you so much, this was very helpful as I'm making my first game!
You're welcome!
Great video. Make it look easy. Thanks
You're welcome, Dwayne!
Thanks for the tutorial
You saved my project
Understandable. Have a nice day
Nice video it helped me a lot
I'm glad it helped and thanks for watching :)
you are an amazing human being
No u!
THANK YOU SOOO MUCH MAN IT WORKS PERFECTLY!!!!!!!!!!!!!!!GOD BLESS YOU
i'm thinking the same thing. its cool that this comment section is still alive after so many years of the video being up
thank you this really helped!
you're welcome!
demon time
Know i got that HONONOYNYOYRYRYRYEJTBAHSBFDSBFBDSHFBDF
Thanks man I was having some trouble with my tank racing games with that
You're welcome!
At last, a simple code. Thanks bruv, you're a diamond.
By the way, how would you stop the camera from moving beyond the border when the border of the room has been reached?
I literally coded that for a tutorial video a few days ago, if you can wait a few days.
If you want to try yourself, I just use 2 variables for where I want the camera to be, and then use the "clamp" function to make sure those 2 variables dont go below or above the values I want eg 0 for minimum and room-width - view_width as maximum for the x coordinate
@@GameMakerRob Thanks bro, I'll try this first, and if my single brain cell can't make it work, then I'll wait for that video.
i am a priest. i give blessing to you.
Hi, I speak Spanish and I don't understand any English, but thanks to you I was able to make my view
You're welcome man!
@@GameMakerRob Thank you, I don't know English
Thank you so much man
King Kirb no problem!
when i load the game, the camera moves, except you cant see the player. the camera is also pretty small. Hlw would you go about fixing this?
Edit: it seemed to have fixed itslef when i made the object smaller. But if there anyway to keep the sprite somewhat larger and still have the camera work, please let me know :)
How big is the sprite vs how big is the camera view (in pixels)
Would I be able to make it so there are borders? (like how the default cam has horizontal and vertical borders) if so . . . how?
Skeleton Bro I think the video link in the description is what you're looking for - it makes the camera stop at the rooms borders so that the camera isn't always centred on the player but te player is always in view.
The borders don't have to be based on the room either, you can set them to whatever you like
thanks it worked :D
Hey do you possibly know how to have the camera follow your player if you're at a certain part of the view? Similar style to Super Mario World on snes.
I think this video will be more relevant for you - ua-cam.com/video/9fI91zL_P80/v-deo.html
Instead of using the mouse cursor coordinates, use the player's x and y, and increase the size of the borders so that the player doesn't have to get to the edge before it scrolls
thank you man :D
So, I have a question... I wanted to make a game where the player has two different objects and has different abilities based on the object. I only have two objects for the player, but I cant seem to figure out a way to make the camera follow but one of them. When the player switches, it destroys the other object and creates the new one, but I cant get the camera to follow the new one... I tried making a second view (definitely not the right answer) and haven't found a way to make the object continue to be followed...
Whatever object this code is inside will make the camera follow it, so if you have the code in both objects (but only have 1 of them in the game at the same time) then it should work for both objects.
You could make a camera object that has a target to follow instead, and then change the target that it's following (via a global variable being changed, for example) or you could just have 1 object for both things, with 2 states that you switch between to get the different effects/whatever
@@GameMakerRob Thanks, I got it now though ^^' I made an "object_camera" and made it into a parent for both of my characters, and then set the game camera to follow the "object_camera" which ended with both characters being followed since they adopted the code; thank you for your time though, i was super confused at first and it makes me feel a little better to know that i could get help if I still needed it, thank you for your time! ^^
Circuit of Nightshipping good stuff!
thanks
hi, if you have a problem like me, just move the first two lines of code in the create event.
is there a way to do this with drag and drop?
Thank you !
it just doesn't move with the player at all, dont know what i did wrong
THNX !
You're welcome!
Thanks but it's important to note that unless you plan on changing the size of the camera's view during playtime, you shouldn't calculate half of the view every end step but rather at the initialization or creation event of the player which could help with efficiency. :)
That's very true - it's unnecessary to calculate the size of the view every step. I think it's because I took my code from a zoom object I was using.
@@GameMakerRob When I tried to put it in the creation event, it didn't seem to work but it looks like it runs flawlessly in the End Step at least.
GreetingsField the calculation to determine half the view size? I guess it can depend on what you have going on in your game.
Having the code in the end step is perfectly fine IMO
@@GameMakerRob Oh, right, haha, I assumed the camera position was going to be set there in the same place, haha, no wonder it didn't work. Moving the basic calculation over works fine!
Hi!Thanks bro!I realy appreciate your video but I have one problem...When I go to the world s edge the game glitches.Can u help me?
Edit:I resorved it.I had to make the camera follow the player and to change the horizontal border and the vertical border to 32.Thanks again!
You have to use the relevant gamemaker 1.4 functions like view_xview docs.yoyogames.com/source/dadiospice/002_reference/windows%20and%20views/views/index.html
The camera functions changed with 2.0
not sure if anyone can help me on this.
i used both the movement and the camera follow vids. they work, how ever the player is always off center.
the x/y of the camera is at the top left of the screen, and calculating the camera coordinates should also take into account the sprite_xoffset and sprite_yoffset of the player sprite.
Eg if the player sprite origin is different compared to the one in the video, you'll probably see an off-centre player
Sorry I’m late, but can you answer my question? Are you able to make the camera follow you but when it hits an edge of the room size the camera stops moving but the player is still able to move. I tried the ‘clamp’ but it stopped the camera and the player as well when the camera hit the edge of the level. Please help and thank you.
Hey man, clamp will work, you just need to set it to:
cx = clamp(cx, 0, room_width - camera_get_view_width(view_camera[0]) );
and cy = clamp (cy, 0, room_height - camera_get_view_height(view_camera[0]) );
If you copy/paste this code, check I have enough parantheses etc, its easy to make mistakes typing it in a comment.
I have wondered a couple of times whether I should do a follow up video to show how to clamp the camera like this. The code I gave here will work as long as your "levels / dungeons etc" are confined to the width/height of the room
Hey thanks pal. I didn’t think you would help, especially this quick, to my comment since this video came out two years ago. I really appreciate the help.
@@redoned.8446 NP man, I always try to reply if I can. After I finish the Isometric Tactics Tutorial series I'm doing the extra camera stuff will probably be the first thing I do
Sorry to ask, but what is ‘cx’ and ‘cy’? Were these used in another video? I’m just a little confused.
REDONED. ? Yes, actually. cx = x - halfViewWidth;
cy = y - halfViewHeight;
//now put the clamp code
//now set the camera to cx/cy
Basically we're getting the coordinates to center the camera on the player, then clamping those coordinates to make the view stay inside the room, then setting the view to those coordinates
Tysm
Great stuff. but can I just ask. why only play the music in the left ear?
Alien Anthony I had no idea that was even happening lol...
Sorry for all the requests, but I was wondering if you were going to my a dialogue system tutorial
King Kirb dialogue like in the MyRPG video?
GameMaker Rob Yeah
King Kirb maybe next week I have to finish another video first ;)
First camera I set up was based on a super complicated 3d one.
Wtf young me why didnt I find this.
LuckyOwl lol if you wanna teach me about a 3D camera that's something I haven't mastered yet
GameMaker Rob dont remember it, I left gamemaker for school for a while, I think there are tutorials on it somewhere though
Nice
thank you :)
hello, i followed your tutorial but my character's sprite gets a little distorted. do you know how to solve?
it's a bit too vague to know the cause of your problem. Maybe it's an aspect ratio problem? This tutorial is just a camera tutorial though, and doesn't touch this.
Are you running the game in full screen? If so, does the problem still happen in windowed mode?
@@GameMakerRob no, my game is not in full screen. if this is a ratio problem, do you have any idea how I can solve it? (I know the tutorial isn't about that, sorry. The camera works very well by the way.)
@@skillfenix1570 when you set your camera view, the "ratio" between the view and the port need to match. Eg if your view of the room is 200x400 and then your game screen is 300x600, you will see some weird sprites
hello, i am making an platformer and only want the camera to move on the x-axis is this possible with your code
Try using it without adjusting the y of the camera (although you'll want to set things up so the player starts within the view).
You might also still want the y to change but only when the player gets within a certain distance of the top/bottom of the view.
@@GameMakerRob i just changed this: camera_set_view_pos(view_camera[0], x - halfviewwith , 237);
Bruh do I have to do something special to make the camera functions in GMS1? Cuz for me it's just grey text.
You have to use the gamemaker 1.4 functions like view_xview - docs.yoyogames.com/source/dadiospice/002_reference/windows%20and%20views/views/index.html
in my game i have a moth that follows my mouse cursor when i click the right button. i used this code and it works but i notice that when my moth gets to the edge of the room a duplicating clipping thing happens on the outside border of the room. like the background trys to do that mouse trail thing like on old windows xp computers. any way i can fix this?
Sounds like there's no background. Check the settings in your room.
@@GameMakerRob I have a background of a brick wall. It's in the background tab
@@gardud7 is the size of the view greater than the size of the room?
@@GameMakerRob the size of the view is smaller but it seems that since the camera is keeping the character in the center the player is allowed to go to the very edge of the screen so the camera view goes past the background image. The size is the same as your video. I would need to have it set up in a way that allowed the camera edge to stop at the edge of the room and allow the player to move away from the center of the camera.
@@gardud7 is the camera clamped to the width/height of the room?
Can you tell me why my room is now infinite?
Rooms are always infinite, even if you make it a 1x1 pixel size. Where you allow the camera to move/ camera size is what limits what you see in-game.
Mine is still jittery... How do you fix this?
Also, thank you so much for the tutorial!
You can just have the code in the player step. As long as the camera is being set after the players moves then you should escape the jitteriness
@@GameMakerRob I tried making it before and after, and in the player step, before and after. none of them worked :(
@@xx_yourmomplayz69xd_xx61 you can just put the code in the player object, after it moves
thanks alot
You're welcome!
Chungus cursor. Great tutorial but why on End Step? :]
so that the player has already moved before the camera is updated. As long as the code runs after the player movement (eg on the lines below it in the same event) it's all good
I did everything you just did and the game crashed on startup
FATAL ERROR in
action number 1
of Step Event2
for object obj_player:
Variable obj_player.halfviewheight(100004, -2147483648) not set before reading it.
at gml_Object_obj_player_Step_2 (line 10) - camera_set_view_pos(view_camera[0], x - halfviewwidth, y - halfviewheight);
Robert Chambers the message says that halfviewheight wasn't set before GM tried to read it.
Check the spelling of the variables when you set them. If you did what I did exactly you shouldn't have any crashes (or else I would have had them too)
@@GameMakerRob ahh gotcha. Looks like I forgot to capitalize them.
Robert Chambers if you're using GM2 there's a yellow triangle with an exclamation mark if a variable is only used once. It's a good way to tell if you've made a typing mistake or not.
how would I make it follow the mouse?
Instead of using the player coordinates, use mouse_x and mouse_y
@@GameMakerRob I use the create event right?
@@ImFrAlex No, the create event only runs once - when an instance is first created. To make things happen all the time, the step event is usually the best place to do it.
There are usually 60 steps in a second in gamemaker rooms, and that's where you want to be checking the mouse coordinates and moving the camera accordingly
i get an error about the line "#region" anyone knows why?
What does the error say? If it's a red mark on the same line it might mean you're missing an #endregion to close it off
it might be because you're using an older version of game maker Like: game maker studio 1 Or game maker 8
i'm sorry, i'm new to this and need help. At 3:10 there's a square that appears to the left of #region with lines going up and down. It didn't appear for me when i wrote this code. Am I supposed to do something to make it appear?
i love you
Yeah Object following just did not work for me, this is a great solution
mine just worked like this:
#region MOVE THE VIEW
halfviewWidth = camera_get_view_width(view_camera[0]) / 2.5;
halfviewHeight = camera_get_view_height(view_camera[0]) / 1.5;
camera_set_view_pos(view_camera[0], x - halfviewHeight, y - halfviewWidth);
#endregion
didnt work.
fala de cuo i ne cuo
The intro is fucked up : the sound is only in the left ear...
sorry about that :)
don't work
It's more likely that you implemented it incorrectly
I'm having trouble because of
ERROR in
action number 1
of Step Event2
for object Obj_Player:
Variable .halfViewHeigh(100012, -2147483648) not set before reading it.
at gml_Object_Obj_Player_Step_2 (line 6) - camera_set_view_pos(view_camera[0], x - halfViewWidth, y- halfViewHeigh);
############################################################################################
gml_Object_Obj_Player_Step_2 (line 6) does anybody have tips
@@Beetroo3 you're missing a "t" on the end of halfViewHeigh
@@GameMakerRob Thank you the camera works exactly how I wanted it to.
I am thankful you respond to comment on 5 year old videos, ill have to check the other camera videos
@@Beetroo3 lol np