Running - Slow to fast

Hi yall

i would like to ask what i shall do to make my character move from 0 velocity to 2 as max ( for example) while moving to any direction then slowly stops to 0 velocity if no key is pressed ? Should i change the movement speed with key holding duration or is there anything better than it?

thanks yall

I do this all the time.

The way I do it is with properties. I’ll typically have three movement-based props on the object that needs this gradual movement (for purposes of keeping everything simple, let’s say it’s the Player object). These props are “speed”, “maxSpeed”, and “acceleration”. They should all be floats. “speed” should stay at 0.0, “maxSpeed” should obviously be the maximum speed you can reach, and “acceleration” should be the rate at which the player will speed up/slow down.

Now I’ll attach an “always” sensor to my walk script (though it’s usually my only sensor since I get all my keypresses from the system instead of a keyboard sensor). I’ll usually have my code set up like this, so alter it to your liking:



###THIS IS PSEUDOCODE, PRETEND THE VARIABLES ARE ALL DECLARED###

    if walkKeyPressed:
        if speed < maxSpeed:
            speed += acceleration
        else:
            speed = maxSpeed
    else:
        if speed > 0.0:
            speed -= acceleration
        else:
            speed = 0.0
    
    own['speed'] = speed
    currentWalk = [0, speed, 0]    #Depending on your selected method of applying motion to your player character; this might be Python but it might also be a logic brick, I just don't know =P

That should be it, poke me if you need help Rocky!

By the way I freakin’ LOVE Keisha so far. HUGELY inspirational for me.

thanks MichaelSea ^.^

It looks good i guess o.o I will try to apply it to the character

You can also try using linear interpolation (lerp) to give smooth acceleration. It’s more difficult to code but gives very nice results.
Remember though, if your character runs in to a wall they need some code to reduce all numbers to zero or you’ll get some strange activity when bumping in to things.

thanks smoking mirror.
hm i dont know how to smoothen the acceleration using lerp. i only use lerp for moveing object from position A to position B. How is your suggestion exactly? o.o teach me teach me ^.^