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

Legacy GM [SOLVED] Fixing going diagonally faster in top-down game makes camera movement lag

C

CornerLord

Guest
Hello.
I'm making top-down game and when I "fix" speed when moving diagonally it makes camera lag if I move diagonally. I have obj_camera and I have set view to follow it in room settings. I have also tested without camera object but it doesn't help.

Here is my code for player movement:
Code:
//Player Movement
hspeed = walkingSpeed * (right - left);
vspeed = walkingSpeed * (down - up);

// Speed fix when pressing 2 buttons at the same time
if abs(hspeed)==abs(vspeed)
    speed = abs(hspeed);
I have tried to use
Code:
if speed >= walkingSpeed {speed = walkingSpeed }
but it doesn't help.
 
G

Gillen82

Guest
Had a similar problem with the same thing. Here's the code that I used as a solution to the problem. Maybe you could use something similar?

Code:
if(up && left) || (up && right) || (down && left) || (down && right) {
    mspd = 3;
}else{
    mspd = 4;
}
N.B mspd is variable to determine how fast the character can move. Default is set to a speed of 4, but, when the character is moving in an angle, the speed is reduced to 3. Seems to work OK, although there's probably a better solution. Hope this helps!!
 
C

CornerLord

Guest
Had a similar problem with the same thing. Here's the code that I used as a solution to the problem. Maybe you could use something similar?

Code:
if(up && left) || (up && right) || (down && left) || (down && right) {
    mspd = 3;
}else{
    mspd = 4;
}
N.B mspd is variable to determine how fast the character can move. Default is set to a speed of 4, but, when the character is moving in an angle, the speed is reduced to 3. Seems to work OK, although there's probably a better solution. Hope this helps!!
Code:
hspeed = walkingSpeed * (right - left);
vspeed = walkingSpeed * (down - up);

if(up && left) || (up && right) || (down && left) || (down && right) {
    walkingSpeed = 2.825;
}else{
    walkingSpeed = 4;
}
This works fine but maybe not the best solution xD I have to use 2.825 instead of 3 because if I use 3 then the speed is 4.24 when moving diagonally (+0.24 to normal speed) :/

Edit: It doesnt work... Camera lags still when moving diagonally :(
 

Bingdom

Googledom
The problem is that the camera is moving to the original spot of the player, then the player moves.
Put the code in the END step event.

What I do is move the view inside the player's code, after the movement scripts.
 
C

CornerLord

Guest
The problem is that the camera is moving to the original spot of the player, then the player moves.
Put the code in the END step event.

What I do is move the view inside the player's code, after the movement scripts.
I got your point but can you tell me how to control view inside the player's code :)

And here is my obj view's code
Code:
x += (obj_player.x-x)/5;
y += (obj_player.y-y)/5;

EDIT: I got it :) I put
Code:
view_xview[0]=x-(view_wview[0]/2);
view_yview[0]=y-(view_hview[0]/2);
to end step event in player object. It works now.
But my new problem is how to make it "smooth" because now I can't use the settings in room editor :(
 
Last edited by a moderator:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Use lengthdir_x/y to do the movement rather than using hardcoded values...

Code:
var hspd = right - left;
var vspd = down - up;
x += lengthdir_x(walkingSpeed, hspd);
y += lengthdir_y(walkingSpeed, vspd);
 

Bingdom

Googledom
But my new problem is how to make it "smooth" because now I can't use the settings in room editor
lerp() is a really good function for this! It would even make it more smooth than the built-in room camera.
Code:
var targX = x-view_wview[0]/2;
var targY = x-view_hview[0]/2;

view_xview = lerp(view_xview, targX, 0.1);
view_yview = lerp(view_yview, targY, 0.1);
 
C

CornerLord

Guest
Thanks for everybody who helped me! :)

And btw
var targY = x-view_hview[0]/2; should be
var targY = y-view_hview[0]/2; @Bingdom
 
S

Stef

Guest
xaxis = keyRight - keyLeft OR xaxis = gamepad_axis_value(0, gp_axislh);
yaxis = keyDown - keyUp OR xaxis = gamepad_axis_value(0, gp_axislv);

var dir = point_direction(0, 0, xaxis, yaxis);

hspd = lengthdir_x(abs(xaxis * moveSpeed), dir);
vspd = lengthdir_y(abs(yaxis * moveSpeed), dir);

x += hspd;
y += vspd;
 
Top