• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Only keeping the view inside the room horizontally

X

xery555

Guest
Hi!

I have a problem with the view function in Game Maker. I want the view to not go outside the room horizontally, but ignore the room height. It should follow the player.

So I have this code:
view_xview[0] = x - (view_wview[0]/2);
view_yview[0] = y - (view_hview[0]/2);
But that makes the view go outside the room freely from all sides.

Then I have his code:
view_hborder[0] = view_wview[0] / 2;
view_vborder[0] = view_hview[0] / 2;
But that makes the view not go outside the room at all.

So I guess the solution is a mix of these two, but I have no idea how to do it.
 
Hi!
I'd like to know more about the issue, so I can maybe help :D

I did a similar thing on the project I'm working on.
if(keyboard_check(vk_up))y-=50;
if(keyboard_check(vk_right))x+=50;
if(keyboard_check(vk_down))y+=50;
if(keyboard_check(vk_left))x-=50;

view_xview=obj_CameraMan.x;
view_yview=obj_CameraMan.y;

Note that with this setup you DO NOT want to set obj_CameraMan as the "Object being followed" in the view Tab.
In this way, the view is free to move outside the room border if you move it via code.

Does the view cover the entire room? And you only need it to move up and down?
In this case, it's pretty simple, you will only move the view_yview when needed.

Or is there an amount of space it can travel horizontally?
In this case instead, you want to check if the view has reached the border.
Let's say you want the view to travel horizontally within a 200 Pixel Range (100 to the left, and right as well), supposing it starts in the middle of the room.
You want to store the initial position of the view, start_x=view_xview;

You can change the x position when needed, and check if the current position is greater than the start position + 100. Or smaller than the start position - 100.
if(view_xview>start_x+100)view_xview=start_x+100;
and
if(view_xview<start_x-100)view_xview=start_x-100;

And move the y when needed just as in the first case.

If I can help in any way or I wasn't clear let me know! I'll be glad to help you out :D
 
Last edited:
Top