• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Problem with Camera

M.G.

Member
Hello, I have a large room for my game, the camera always moves slightly depending on where the player is. Actually everything works perfectly.

The only problem is that the camera is not aimed at the player from the start, but first searches for him in the room. It is the same when the player is recreated at a checkpoint.

Here is the code:

obj_camera

The Creat_Event:

target_x = 0;
target_y = 0;

spd = 10

view_camera[0] = camera_create_view(0,0,256,224,0,-1,-1,-1,0,4)

The Step Event:

target_x = (oPlayer.x div camera_get_view_width(view_camera[0])) * camera_get_view_width(view_camera[0]);
target_y = (oPlayer.y div camera_get_view_height(view_camera[0])) * camera_get_view_height(view_camera[0]);

if (abs(x - target_x) < spd)
{
x = target_x;
}
else
{
if (target_x > x)
{
x += spd;
}
else if target_x < x
{
x -= spd;
}
}
if (abs(y - target_y) < spd)
{
y = target_y;
}
else
{
if (target_y > y)
{
y += spd;
}
else if target_y < y
{
y -= spd;
}
}

camera_set_view_pos(view_camera[0],x,y)


I hope someone knows the problem.
 

flyinian

Member
Hello, I have a large room for my game, the camera always moves slightly depending on where the player is. Actually everything works perfectly.

The only problem is that the camera is not aimed at the player from the start, but first searches for him in the room. It is the same when the player is recreated at a checkpoint.

Here is the code:

obj_camera

The Creat_Event:

target_x = 0;
target_y = 0;

spd = 10

view_camera[0] = camera_create_view(0,0,256,224,0,-1,-1,-1,0,4)

The Step Event:

target_x = (oPlayer.x div camera_get_view_width(view_camera[0])) * camera_get_view_width(view_camera[0]);
target_y = (oPlayer.y div camera_get_view_height(view_camera[0])) * camera_get_view_height(view_camera[0]);

if (abs(x - target_x) < spd)
{
x = target_x;
}
else
{
if (target_x > x)
{
x += spd;
}
else if target_x < x
{
x -= spd;
}
}
if (abs(y - target_y) < spd)
{
y = target_y;
}
else
{
if (target_y > y)
{
y += spd;
}
else if target_y < y
{
y -= spd;
}
}

camera_set_view_pos(view_camera[0],x,y)


I hope someone knows the problem.
First Thought:

Try this,

GML:
Create Event:

target_x = 0;
target_y = 0;

spd = 10

view_camera[0] = camera_create_view(0,0,256,224,0,-1,-1,-1,0,4)


target_x = (oPlayer.x div camera_get_view_width(view_camera[0])) * camera_get_view_width(view_camera[0]);
target_y = (oPlayer.y div camera_get_view_height(view_camera[0])) * camera_get_view_height(view_camera[0]);
If oPlayer doesn't exist error appear. Use an instance_exist() function with the last target_x/y. May need one in the step event as well which may not fix your problem.



Then let us know of the results.
 
Last edited:

M.G.

Member
Many thanks for your answer ..., but it doesn't help.

I think the main problem is this code:

"camera_set_view_pos(view_camera[0],x,y)"

It doesn't matter what is in the createvent ... he lets the camera search again.

But it is absolutely necessary because it controls the room change.
 

flyinian

Member
Many thanks for your answer ..., but it doesn't help.

I think the main problem is this code:

"camera_set_view_pos(view_camera[0],x,y)"

It doesn't matter what is in the createvent ... he lets the camera search again.

But it is absolutely necessary because it controls the room change.
Yeah, I don't think I know. I'd imagine it's a simple x/y update but, it's probably much more complex than that.

- Retracted -
 
Last edited:

Vusur

Member
Try putting the code in END STEP EVENT instead of STEP EVENT.

Your camera is lagging behind. You didn't notice it, because of how you calculate the camera position, but room_start and checkpoints change the position so much, that you encounter this problem.

My guess, when you do this:

GML:
target_x = (oPlayer.x + camera_get_view_width(view_camera[0] / 2);
target_y = (oPlayer.y + camera_get_view_height(view_camera[0] / 2);
which is ofc a different camera, where the player is strictly centered, you should notice a small lag from the camera while moveing and then stopping.

A lot of people have this problem. Within a step cycle, the order of instances during the step event is not set in stone. We don't have control over it.
Think about this way:
What happens, if the player step is performed first, the player got new coords and then the camera step is performed, it uses these coords. The camera step has the newest coordinates, everything is fine. But now change the order. The camera step is performed first, it uses the current coords of the player and then the player step is performed, where the player gets new coords. With this order, the camera position is always calculated with "old" coords and can only catch up the next step cycle. END STEP would make sure, that the camera always gets the newest coords from player, because END STEP is always performed after STEP.

The sad thing about this "fix", it probably destroys your current camera behaviour for your normal gameplay. If I'm correct, the camera is fixed, the player can move around in this "screen" and once he leaves the screen, the gamera follows the player? Like a catch-up? The fix would destroy this. Once the player leaves the screen, the camera is set immediately. If your current camera behaviour for your normal gameplay is like this and you really like it, let me know. This would need some extra lines. Like flags (boolean) that tells you, if its a screen change or if it's a teleport.
 
Top