Discuss Scratch

SL3R
Scratcher
2 posts

Need to fix diagonal movement

Diagonal movement is faster than normal movement and idk how to fix it

not sure how to post images but the script is

when green flag clicked
forever
(if key w pressed) or (if key up arrow pressed) then
change x by 4


and its like that for all the directions
Met4Knight
Scratcher
100+ posts

Need to fix diagonal movement

if you move up 1 pixel and then right 1 pixel, it like if you walked on the diagonal of a 1 pixel wide square, and you probably know that the diagonal is longer than the side of a square, i'm gonna simplify but basically, to get the length of the diagonal of a square given the length of its side, you multiply its side by √2 (you can find that using pythagoras' theorem), so to fix the issue, whenever you move, instead of directly changing the position of the character, you can change a variable that corresponds to his speed, and if you move diagonally, then you already know that it will move too fast for what it is supposed to do, so you will apply a fix to the speed variables so that it matches the normal speed again
when green flag clicked
set [speed v] to [5] // you can change that value to whatever you want, it just says how fast the player will move
set [x spd v] to [0]
set [y spd v] to [0]
set [dist v] to [0]
go to x: (0) y: (0)
forever
set [x spd v] to ((<key [right v] pressed?> - <key [left v] pressed?>) * (speed)) // weird trick using the fact that boolean inputs (the hexagon shaped blocks) can be considered as numbers (false = 0 and true = 1)
set [y spd v] to ((<key [up v] pressed?> - <key [down v] pressed?>) * (speed))
set [dist v] to ([sqrt v] of (((x spd) * (x spd)) + ((y spd) * (y spd)))::operators)// this is how far the player will move, found using pythagoras' theorem
if <(dist) > (speed)> then // if the distance move is too great, then the player is probably moving diagonally
set [x spd v] to ((x spd) / (dist)) // weird thing that "adapt" the variables to diagonal movement, so that the player no longer moves too far
set [y spd v] to ((y spd) / (dist))
end
change x by (x spd) //finally we move the player
change y by (y spd)
end

Last edited by Met4Knight (Aug. 14, 2025 16:33:24)

jjspidermanmcqueen
Scratcher
19 posts

Need to fix diagonal movement

Hey but it's Not The Best like if your making a RPG thats a difreint Story! heres Some help ou need to do some math and and the dictance tell me if you need me to make it! But its okay if you found the problem and fixed it
bsteichman
Scratcher
500+ posts

Need to fix diagonal movement

multiplying by the square root of 2 will give an equal speed

forever
set [moveX v] to ([movement speed] * (<key [right arrow v] pressed?> - <key [left arrow v] pressed?>))
set [moveY v] to ([movement speed] * (<key [up arrow v] pressed?> - <key [down arrow v] pressed?>))
if <<not <(moveY) = [0]>> and <not <(moveX) = [0]>>> then
set [moveX v] to ((moveX) * ([sqrt v] of (2))
set [moveY v] to ((moveY) * ([sqrt v] of (2))
end
change x by (moveX)
change y by (moveY)
end

what this does and how it works
first two blocks are basically the same as your old code, but now set a variable first, instead of moving right now.
true false blocks become 0/1 blocks when placed, so true-false = 1 and true-true = 0

The if block shrinks the movement amount if you're moving diagonally.

“movement speed” would probably be 4 for you

Last edited by bsteichman (Aug. 14, 2025 22:55:07)

jjspidermanmcqueen
Scratcher
19 posts

Need to fix diagonal movement

bsteichman wrote:

multiplying by the square root of 2 will give an equal speed

forever
set [moveX v] to ([movement speed] * (<key [right arrow v] pressed?> - <key [left arrow v] pressed?>))
set [moveY v] to ([movement speed] * (<key [up arrow v] pressed?> - <key [down arrow v] pressed?>))
if <<not <(moveY) = [0]>> and <not <(moveX) = [0]>>> then
set [moveX v] to ((moveX) * ([sqrt v] of (2))
set [moveY v] to ((moveY) * ([sqrt v] of (2))
end
change x by (moveX)
change y by (moveY)
end

what this does and how it works
first two blocks are basically the same as your old code, but now set a variable first, instead of moving right now.
true false blocks become 0/1 blocks when placed, so true-false = 1 and true-true = 0

The if block shrinks the movement amount if you're moving diagonally.

“movement speed” would probably be 8 for you
SL3R
Scratcher
2 posts

Need to fix diagonal movement

Thanks for the comments guys, i fixed it
jjspidermanmcqueen
Scratcher
19 posts

Need to fix diagonal movement

Your Welcome, SL3R

Powered by DjangoBB