Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Top-down movement based game "dash ability" bug
- blobster01
-
Scratcher
18 posts
Top-down movement based game "dash ability" bug
Hi, forums! I've been having an issue in my ‘game’ (you can only move the character right now with WASD, nothing more), where the dash ability, used by pressing the spacebar, is bugged. When the spacebar is pressed, the character is propelled forward, which is all fine and dandy, but there is one thing I've observed wrong with it: when other directions are pressed, sometimes the dash will happen again without the spacebar being hit (just spam WASD after dashing to see this). I have made attempts to fix this by trying a few different things, such as replacing the if with an if-else, as shown in the image below.

Needless to say, it didn't work.
Here's the link to the project itself. There is some code in the backdrop regarding the game loop, by the way. Thanks in advance for the help

Needless to say, it didn't work.
Here's the link to the project itself. There is some code in the backdrop regarding the game loop, by the way. Thanks in advance for the help

- sashimiricedev
-
Scratcher
100+ posts
Top-down movement based game "dash ability" bug
I think it might be better game design to dash the player in the direction of their motion.
Basically, we can get a normal direction (meaning that the strength of the direction is one) and then multiply THAT by our dash speed. This way our dash should function in an intuitive way and make it easier for the player.
We can think of a direction, or vector, as a rope. The length of the rope is called the MAGNITUDE, and where the rope goes is the DIRECTION.
Despite the math sounding hard, it's actually quite easy. Basically, we just need to divide the direction (in this case, the players move direction) by their MAGNITUDE (the length of the rope).
Lets go ahead and get the move direction
set to ( of (((movedir x) * (movedir x))+((move dir y) * (move dir y))))
And then divide the xvel and yvel by this length
The last step is to multiply BOTH of them by our DASH SPEED
All done! Ask me if you have any questions.
Basically, we can get a normal direction (meaning that the strength of the direction is one) and then multiply THAT by our dash speed. This way our dash should function in an intuitive way and make it easier for the player.
We can think of a direction, or vector, as a rope. The length of the rope is called the MAGNITUDE, and where the rope goes is the DIRECTION.
Despite the math sounding hard, it's actually quite easy. Basically, we just need to divide the direction (in this case, the players move direction) by their MAGNITUDE (the length of the rope).
Lets go ahead and get the move direction
set [movedir x v] to (<key [d v] pressed ? > - <key [a v] pressed>)We can get the length of the rope with this simple formula
set [movedir y v] to (<key [w v] pressed ? > - <key [s v] pressed>)
set to ( of (((movedir x) * (movedir x))+((move dir y) * (move dir y))))
And then divide the xvel and yvel by this length
set [xvel v] to ((movedir x) / (length of rope))Now, the length of our MOTION vector is now exactly 1.
set [yvel v] to ((movedir y) / (length of rope))
The last step is to multiply BOTH of them by our DASH SPEED
set [xvel v] to ((xvel) * (dash speed))
set [yvel v] to ((yvel) * (dash speed))
All done! Ask me if you have any questions.
Last edited by sashimiricedev (April 26, 2024 17:57:34)
- blobster01
-
Scratcher
18 posts
Top-down movement based game "dash ability" bug
Thanks for the timely reply, sashimiricedev! Apologies for the, well, almost year late response lol–I've just returned to this project after not using Scratch for quite a while. Anyway, while your explanation makes sense, and I understand the concept of having the player dash into their direction, I'm having a difficult time with the actual implementation of this idea, more specifically where to use the idea of setting the movedir x / magnitude and movedir y / magnitude in the project. Here is what I have so far: https://scratch.mit.edu/projects/1008615215/. It appears to function somewhat decently, but again, implementing the two blocks mentioned previously has given me some trouble. Thanks again!
- Discussion Forums
- » Help with Scripts
-
» Top-down movement based game "dash ability" bug