GMS 2 Check if Camera is outside the room

heirey

Member
Hello,

I use this code in the end-step event of my Hero object to move the camera on my top-down game:
Code:
var targX = x-camera_get_view_width(view_camera[0])/2;
var targY = y-camera_get_view_height(view_camera[0])/2;

camera_set_view_pos(view_camera[0], lerp(camera_get_view_x(view_camera[0]), targX, 0.1), lerp(camera_get_view_y(view_camera[0]), targY, 0.1));
but i dont find how block it if the camera is outside the room, the camera continues to scroll the view out of room... can you help me?

Thanks!!
 
P

PandaPenguin

Guest
Ok thanks, can you give me an example of the code with this function? i need to clamp targX and targY?
yes, I would clamp() those

Code:
var targX = x-camera_get_view_width(view_camera[0])/2;
targX = clamp(targX, 0, room_width()-camera_get_view_width(view_camera[0]))
 

heirey

Member
yes, I would clamp() those

Code:
var targX = x-camera_get_view_width(view_camera[0])/2;
targX = clamp(targX, 0, room_width()-camera_get_view_width(view_camera[0]))
Thanks! Here the final code:
Code:
var targX = x-camera_get_view_width(view_camera[0])/2;
targX = clamp(targX, 0, room_width-camera_get_view_width(view_camera[0]));

var targY = y-camera_get_view_height(view_camera[0])/2;
targY = clamp(targY, 0, room_height-camera_get_view_height(view_camera[0]));

camera_set_view_pos(view_camera[0], lerp(camera_get_view_x(view_camera[0]), targX, 0.05), lerp(camera_get_view_y(view_camera[0]), targY, 0.05));
 
There is a new Camera tutorial available from YYG which covers this... I know you say you've solved it, but it might still be worthwhile checking it out. :)

https://marketplace.yoyogames.com/assets/5153/cameras-and-views
I am working through this tutorial, and have run into an issue around the Zooming section of it. Right now it has variables for "ZoomF" and "_zoomLerp" that are never declared. It also has you set a "zoom" variable in the create step of the obj_player, that never gets used.

...As I was writing this, I reviewed the screenshot at the end of the tutorial and found that "_zoomLerp" was set to "_lerpH", so I made that change. I then set the "zoom" variable in the create event to "zoomF" and that fixed the issue I was having.

I hope this helps anyone who might have run into this issue.
 
Top