View/camera preceding player

Palocles

Member
I’ve been following Shaun’s tutorials and have my camera following behind the players character sprite.

How would I change that code to get the camera to be x pixels in front of the player sprite?

I’d like to make the camera be 10 pixels in front when the character walks and 25 when they run. Or something similar.

Thanks.
 

Robzoid

Member
Shaun has it set up so that the oCamera object is in the middle of the view, so you can just set the oCamera's x value to be greater than the player's by a certain amount.

For example in the oCamera object's step event: x = oPlayer.x + 25;

Also, I forget if Shaun Spalding does this, but I like to use the end step event for the oCamera object. It ensures that the player object's movements have been calculated before you calculate your camera's position.
 

TheouAegis

Member
Inside player End Step event:
Code:
camx = x + image_xscale * sign(abs(hspeed)) * (10 + running * 15);
cam_speed = camx - camera_get_view_x(view_camera[0]);
if abs(cam_speed) & ~1
    cam_speed = cam_speed >> 1;
camera_set_view_pos(view_camera[0], camera_get_view_x(view_camera[0]) + cam_speed, camera_get_view_y(view_camera[0]));
Try that. I haven't tested it, just an idea that popped in my head on my way home from work last night.
(final edit at 3:15pm PST)
 
Last edited:
Top