• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

Legacy GM [SOLVED] Smooth View + Parallax Background

knru

Member
I'm new to GameMaker and have very little experience on coding in general. English is not my first language, but I hope you can understand what I'm after.

So, I want a parallex background that moves smoothly with my player. My problem is that every time my character stops, my background kind of.. lags? Here's a video.

My view follows a "obj_dummy" object, that follows my player. I used this tutorial to make the parallex background and put the commands (background[0]=round(view_xview /1.2)....etc) in my player's step event. This is the tutorial I used for player movement.

The problem seems to be related to the character's movements, since the character kind of goes a few pixel back after you release you key. I tried fixing this with distance_to_object command, but as you can see from the video, it doesn't help. I also figured maybe telling obj_dummy to not move if player has moved under, for example, 2 pixels, but I can't seem to figure out how to do it.

I have tried solving this myself and googling a lot, but I can't seem to find an answer. Does anyone have any ideas how to fix this? :(

My code for obj_dummy following player (obj_hero_pf) in obj_dummy's step event
Code:
if (distance_to_object(obj_hero_pf) < 10)
{c_spd = 0;}
else
{
c_spd= round((obj_hero_pf.x-x)/20);

}

if ( instance_exists(obj_hero_pf)){
obj_dummy.y = obj_hero_pf.y-30;
obj_dummy.x += c_spd;
}

My player movement + parallax background in obj_hero_pf's step event
Code:
//player’s input
key_right = keyboard_check (vk_right);
key_left =  -keyboard_check (vk_left);
key_jump = keyboard_check_pressed (vk_space);
key_run = keyboard_check (vk_shift)

//React
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_wall))
    {
    vsp = key_jump * -jumpspeed
    }

//Horizontal Collision
if place_meeting(x+hsp,y,obj_wall)
{
    yplus = 0;
    while (place_meeting(x+hsp,y-yplus,obj_wall) && yplus <= abs(1*hsp)) yplus += 1;
    if place_meeting(x+hsp,y-yplus,obj_wall)
    {
        while (!place_meeting(x+sign(hsp),y,obj_wall)) x+=sign(hsp);
        hsp = 0;
    }
    else
    {
        y -= yplus
    }
}
x += round(hsp);

//Vertical collision
if (place_meeting(x,y+vsp,obj_wall))
{
while (!place_meeting(x,y+sign(vsp),obj_wall))
{
 y+= sign(vsp);
}

vsp = 0;
}

y += round(vsp); 

if (key_run)
{
movespeed = 5;
}
 else {
 movespeed = 3;
 }


 //Walk area
x = clamp(x, 16, room_width-16); 
y = clamp(y, 60, room_height); 

///background
background_x[0] = round(view_xview /1.2);
background_x[1] = round(view_xview /2.2);
 

Bayesian

Member
You can try setting obj_dummy's x/y from inside the player object using a with statement. Make sure to set this after the player has finished moving. I've seen many times objects updating out of sync for some reason when they rely on each other and this is what I've done to fix it.
 

knru

Member
You can try setting obj_dummy's x/y from inside the player object using a with statement. Make sure to set this after the player has finished moving. I've seen many times objects updating out of sync for some reason when they rely on each other and this is what I've done to fix it.
Didn't work. :( I think obj_dummy is in sync with the player. The player just moves back a few pixel back after I release the arrow key and obj_dummy follows that as well.
 

knru

Member
I got it working now! It was easier than I thought, I just changed from build-in view to GML smooth view.

Here's the code I have in obj_dummy's step event:
Code:
if ( instance_exists(obj_hero_pf)){

//View follow
view_xview[0] += ((x-(view_wview[0]/2)) - view_xview[0]) * 0.06 
view_yview[0] += ((y-(view_hview[0]/2)) - view_yview[0]) * 0.06 
//Clamp view
view_xview[0] = clamp(view_xview[0],0,room_width-360);
view_yview[0] = clamp(view_yview[0],0,room_height-270);

obj_dummy.y = obj_hero_pf.y-30; 
obj_dummy.x = obj_hero_pf.x;

}

///background
background_x[0] = view_xview/1.2;
background_x[1] = view_xview/2;
 
Top