• 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!

[Solved] Camera Object Panning

J

Joltout

Guest
So basically, instead of having my games camera follow my character, it's following an invisible object that is created on top of my character that's currently set at x/y = character's.x/y.

But upon creation, I would like it to pan ahead of the character x distance, but smoothly with deceleration, stopping at a precise +x distance away from the character and then panning smoothly the other way to -x when the characters direction changes (this is a platformer btw and y doesn't matter at this stage).

So I have a global variable set up that is linked to the characters direction variable (and image_xscale), "global.previous_dir = previous_dir;", so previous_dir is = to either 1 or -1. Outside of that, I have no idea how I can set up the decel/accel mechanics, final position and recognizing the direction swap for the camera. Any help? I've been stuck on this for a while lol
 

JasonTomLee

Member
What I usually do is set up a TARGET object inside a Camera object, then get the x/y coords from the 'TARGET' ( aka the player ).
You then set a 'XOFFSET' variable inside the Camera to Tween to a SET_XOFFSET value to get that smooth transition.
The direction would be determined by the player's facing direction ( Left or Right ). You could take it a step further and pan the offset more as you move towards the same direction.

Here's some psuedo code- Hope it helps & good luck!

// Create
target = oPlayer;
targetx = 0;
targety = 0;
offx = 0;
offxset = 100;
offx dir = 1; //left or right
facing = 1;

//Step
if ( instance_exists( target )){
targetx = target.x;
targety = target.y;
}

//Step of Player or Cam
if ( keyboard_check( vk_right )){
facing = 1;
} else if ( keyboard_check( vk_left )){
facing = -1;
}

offx = lerp( offx, offxset* facing, 0.1 ); //you can change this to any tween function

view_x = ( target.x + view_w/2 ) + offx;
 
J

Joltout

Guest
I tried to implement the code you gave, messed around with it for a while and tried and failed to do some room boundary clamping (which I've just flat out deleted for now). But for some reason I can only get it to work in reverse even if I change the values of the facing variable, which I just ended up swapping back to a global I'm using instead. But I'm sure I'm misinterpreting some of those variables and causing all of these issues.

However, I added a sprite to the camera object (with some image alpha) and found that it just doesn't move unless I set it so that x = oPlayer.x. But again, I'm sure I'm not understanding something here.

I greatly appreciate the help by the way! :D

Code thus far (for the camera object):

Code:
// Create
target = oPlayer;
targetx = 0;
offx = 0;
offxset = 90;
image_alpha = 0.1;

//Step
if ( instance_exists( target ))
{
    x = oPlayer.x;
    y = oPlayer.y;
}

offx = lerp( 0, offxset * global.previous_dir, 0.1 );

view_xport[0] = ( x + 90/2 ) + offx;
 
Last edited by a moderator:

Slyddar

Member
Sorry, earlier I didn't have time to write a reply, but suggested Nuclear Throne. Here's some code that should do what you need. Place it in the camera object.
Code:
//create
follow = o_player;          //who to follow
move_to_x = x;              //position to move camera to
camera_pan_speed = 0.08;    //how fast camera will pan, 1 = instant
camera_distance = 150;      //how far in front of the player the camera will be
Code:
//move cameras x to follows x slowly
x = lerp(x, move_to_x, camera_pan_speed);

//update position
if follow != noone {
    move_to_x = follow.x + sign(follow.facing) * camera_distance;
}

//move camera
view_xview[0] = x - view_wview[0]/2;
Assuming you are capturing the players facing direction in his step with something like
Code:
if hsp != 0 facing = sign(hsp);
 
Last edited:
J

Joltout

Guest
Ah! That works perfectly! Thank you so much for the help!

I'll go ahead and work on this for the y side of things now, gotta learn this all or I can't progress ;)
 
Top