Hi, I’m fairly new to Gdevelop and I’m trying to make my player move slower when shift is pressed, kind of like in some fps games with the character crouching. I couldn’t find any articles on this. thank you in advance!
How are you moving the player? Because that determines the solution. Are you moving the player x pixels at a time, by a force, using a behaviour?
Show us a snippet of the code you that we can use as a starting point.
Ok, first thing is to create a global variable, call it PlayerBaseSpeed, and default it to 300 - this is many pixels the player moves per second (the 5 you in the action is applied per frame, and at roughly 60 frames per second is about 300 pixels per second).
Next create a global variable, call it PlayerSpeedAdjuster, and set it to 1.
Then, in your action change the add/subtract 5 to add/subtract Globalvariable(PlayerBaseSpeed) * TimeDelta() * GlobalVariable(PlayerSpeedAdjuster).
(Delta time is the fraction of a second that’s elapsed since the last frame update/refresh. If this is new for you, check out the explanation at the bottom of this page)
To slow down or speed up your character, simply modify the PlayerSpeedAdjuster variable. Anything less than 1 is slower, and anything greater than 1 is faster.
So how would I link this to a key? Is it possible to slow down my player while a key is being pressed down and have it return to normal speed once the button is released?
Same way that you are moving the object using the key pressed condition:
the first event has the key pressed condition, and sets the PlayerSpeedAdjuster to a value other than 1.
the second event has the key released condition, and sets the PlayerSpeedAdjuster to 1.
That’s an alternative way, but you have to add those subevents to every movement keypress - up, down left and right. And if you want to change the speed, you then have to change it in at least 4 places. Plus position changes with a fixed value are vulnerable to computer speeds & lag.