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

how do i make my character movement more smoother?

M

milkieyuna

Guest
how do i make my character movement more smoother?

like car speed from slow to fast.

could you please write the coding, i really need some help
 

NightFrost

Member
By that, do you mean like accelerating a car from zero to maximum speed? You'll have to define several variables in the car's Create event: one for current speed, one for acceleration, and one for maximum speed. Every step event, when player presses button to accelerate, as long as current speed is not equal or greater than maximum speed, you add acceleration to current speed. Deceleration would then work by reducing current speed until it is zero.
 

Qlak

Member
Hey!

like car speed from slow to fast.
In my game I'm also accelerating my character when he starts walking, up to his maximum speed. So he is slow at first, but finally moves normally, like a little delay.



could you please write the coding, i really need some help
I achieved it like this:

GML:
// CREATE EVENT:
isCharacterMoving = false; // Flag that checks if character is moving.
movementSpeed = 1;        // Starting speed for the character (your car).
maxMovementSpeed = 3;    // Maximum speed for the character (your car).


// STEP EVENT:
// Acceleration when starting to move:
if(isCharacterMoving) {
    if(movementSpeed < maxMovementSpeed) {
        movementSpeed = movementSpeed + 0.1;
    }
} else {
    movementSpeed = 1;
}
So it works like this:
When isCharacterMoving flag is set to true, character starts moving with speed of 1 and in each frame it goes faster by 0.1 (can be modified so it goes up faster or slower). When his speed achieves 3 (which is maximum speed for me) it stops accelerating and walks with this max speed. If he stops moving his movementSpeed resets back to 1, so when he starts moving again, he needs to gain speed again.

Of course you could also add minimumSpeed and change 0.1 modifier to another variable to make it look better :).
I hope it helps.
 
I'm using this code for my game so that I can tweak acceleration and deceleration.

GML:
// IN YOUR CREATE EVENT
// example values
velocityX = 0 // This value should stay as 0. It represents the objects current velocity
accelerationX = 0.35
deceleration = 0.07
velocityXMax = 7

// IN YOUR STEP EVENT
keyLeft = keyboard_check(vk_left)
keyRight = keyboard_check(vk_right)

if keyLeft and keyRight{
    // do nothing
}
else {
    if keyLeft{
        velocityX -= accelerationX;
    }
    if keyRight{
        velocityX += accelerationX;
    }
}

// Cap maximum x velocity
if ( abs( velocityX ) > velocityXMax )
{
    velocityX = sign( velocityX ) * velocityXMax;
}

// If we have any sort of x velocity
if abs( velocityX ) > 0
{
    // If the x velocity is currently more than the deceleration rate
    if abs( velocityX ) > decelerationX
    {
        // Apply decceleratoin to the velocity
        velocityX -= sign( velocityX ) * decelerationX;
    }
    
    else
    {
        // If the deceleration rate exceeds the velocity, then we just zero it out
        velocityX = 0;
    }
}
 
M

milkieyuna

Guest
Hey!



In my game I'm also accelerating my character when he starts walking, up to his maximum speed. So he is slow at first, but finally moves normally, like a little delay.





I achieved it like this:

GML:
// CREATE EVENT:
isCharacterMoving = false; // Flag that checks if character is moving.
movementSpeed = 1;        // Starting speed for the character (your car).
maxMovementSpeed = 3;    // Maximum speed for the character (your car).


// STEP EVENT:
// Acceleration when starting to move:
if(isCharacterMoving) {
    if(movementSpeed < maxMovementSpeed) {
        movementSpeed = movementSpeed + 0.1;
    }
} else {
    movementSpeed = 1;
}
So it works like this:
When isCharacterMoving flag is set to true, character starts moving with speed of 1 and in each frame it goes faster by 0.1 (can be modified so it goes up faster or slower). When his speed achieves 3 (which is maximum speed for me) it stops accelerating and walks with this max speed. If he stops moving his movementSpeed resets back to 1, so when he starts moving again, he needs to gain speed again.

Of course you could also add minimumSpeed and change 0.1 modifier to another variable to make it look better :).
I hope it helps.

hey!

thanks a lot for your help.

but how you put ur keyboard movement, mine still have a speed like before it was not setting to smoother movement. (sorry im still really beginner in this)
 
M

milkieyuna

Guest
I'm using this code for my game so that I can tweak acceleration and deceleration.

GML:
// IN YOUR CREATE EVENT
// example values
velocityX = 0 // This value should stay as 0. It represents the objects current velocity
accelerationX = 0.35
deceleration = 0.07
velocityXMax = 7

// IN YOUR STEP EVENT
keyLeft = keyboard_check(vk_left)
keyRight = keyboard_check(vk_right)

if keyLeft and keyRight{
    // do nothing
}
else {
    if keyLeft{
        velocityX -= accelerationX;
    }
    if keyRight{
        velocityX += accelerationX;
    }
}

// Cap maximum x velocity
if ( abs( velocityX ) > velocityXMax )
{
    velocityX = sign( velocityX ) * velocityXMax;
}

// If we have any sort of x velocity
if abs( velocityX ) > 0
{
    // If the x velocity is currently more than the deceleration rate
    if abs( velocityX ) > decelerationX
    {
        // Apply decceleratoin to the velocity
        velocityX -= sign( velocityX ) * decelerationX;
    }
    
    else
    {
        // If the deceleration rate exceeds the velocity, then we just zero it out
        velocityX = 0;
    }
}

hey, thank you very much for ur help!

but when i copy ur coding, my character wont move. do you know why?
 
Hey!

In my game I'm also accelerating my character when he starts walking, up to his maximum speed. So he is slow at first, but finally moves normally, like a little delay.

I achieved it like this:

GML:
// CREATE EVENT:
isCharacterMoving = false; // Flag that checks if character is moving.
movementSpeed = 1;        // Starting speed for the character (your car).
maxMovementSpeed = 3;    // Maximum speed for the character (your car).


// STEP EVENT:
// Acceleration when starting to move:
if(isCharacterMoving) {
    if(movementSpeed < maxMovementSpeed) {
        movementSpeed = movementSpeed + 0.1;
    }
} else {
    movementSpeed = 1;
}
So it works like this:
When isCharacterMoving flag is set to true, character starts moving with speed of 1 and in each frame it goes faster by 0.1 (can be modified so it goes up faster or slower). When his speed achieves 3 (which is maximum speed for me) it stops accelerating and walks with this max speed. If he stops moving his movementSpeed resets back to 1, so when he starts moving again, he needs to gain speed again.

Of course you could also add minimumSpeed and change 0.1 modifier to another variable to make it look better :).
I hope it helps.
This is a the "correct" solution, but I would go a step further and "soft-code" the acceleration value on top of the movement speed and max movement speed. The reason is the same as every reason behind not hard-coding values, if you need to change it, it's easily changeable, if you don't, it doesn't affect anything regardless. Something like this:
Create Event:
Code:
movementSpeed = 0;
maxMovementSpeed = 5;
accel = 0.1;
Step Event:
Code:
if(isCharacterMoving) {
    if(movementSpeed < maxMovementSpeed) {
        movementSpeed = movementSpeed + accel;
    }
}
I think it's important to get into the habit of almost always setting up variables instead of using magic numbers, because it gives you a lot more control without sacrificing anything. You might never change your accel through the course of your game development, but if you do, having a variable instead of a magic number will make it so much easier. The number of times I've hard coded something, being entirely sure at the time it would never change, and then later realised "Oh, in order to do X, I need to change the value Y" and then had to search through all my code for every use of Y and turn it into a variable instead of a number is astonishing. Get into the habit early and save yourself future headaches =)
 

Yal

🐧 *penguin noises*
GMC Elder
This is a the "correct" solution, but I would go a step further and "soft-code" the acceleration value on top of the movement speed and max movement speed. The reason is the same as every reason behind not hard-coding values, if you need to change it, it's easily changeable, if you don't, it doesn't affect anything regardless. Something like this:
Create Event:
Code:
movementSpeed = 0;
maxMovementSpeed = 5;
accel = 0.1;
Step Event:
Code:
if(isCharacterMoving) {
    if(movementSpeed < maxMovementSpeed) {
        movementSpeed = movementSpeed + accel;
    }
}
I think it's important to get into the habit of almost always setting up variables instead of using magic numbers, because it gives you a lot more control without sacrificing anything. You might never change your accel through the course of your game development, but if you do, having a variable instead of a magic number will make it so much easier. The number of times I've hard coded something, being entirely sure at the time it would never change, and then later realised "Oh, in order to do X, I need to change the value Y" and then had to search through all my code for every use of Y and turn it into a variable instead of a number is astonishing. Get into the habit early and save yourself future headaches =)
And let's not forget loops that use -1 the max value, so replacing all the "28"s introduces a bug because you missed a "27" somewhere... also you accidentally replaced a 28 that referred to a completely different thing, so now THAT broke, and it'll take weeks before you find it because you don't test play it that often, and you'll think it's a completely different change that caused it....
 
hey, thank you very much for ur help!

but when i copy ur coding, my character wont move. do you know why?
Hi @milkieyuna,

Sorry, this is only a portion of my code that I felt applied to your question. The rest of my code is a little convoluted. As far as why copying and pasting doesn‘t work, it’s because the code I posted is not applying anything to the player’s x variable. So if you did x+=velocityX, the character would start to move. But you’ll notice this code does not have collision checks for walls and such, so you really want to incorporate it into your own code, not completely replace it.
 
C

carlosagh

Guest
This is how i do it

Create Event
GML:
//tweak to your liking
friction=0.05;
throt=0;
handling=2;
minspeed=-2;
maxspeed=5;
Step Event
GML:
//steering
if abs(throt)>0 then image_angle=image_angle+handling*(keyboard_check(vk_left)-keyboard_check(vk_right));
//increase or decrease throttle
throt=keyboard_check(vk_up)-keyboard_check(vk_down);
if throt<minspeed then throt=minspeed;
if throt>maxspeed then throt=maxspeed;
//add a motion vector
motion_add(image_angle,throt);
//keep inside
if x>room_width then x=room_width;
if y>room_height then y=room_height;
if x<0 then x=0;
if y<0 then y=0;
I use the motion_add() function because it makes the movement a bit more realistic than using a static direction and it works great if you're writing your own physics code.
 
Top