Discuss Scratch

Emussa
Scratcher
15 posts

Gravity script for scratch scrolling platformer

I would like to have some gravity for scratch three point 0 that does not sink into the ground
UltraGamer_Studios
Scratcher
96 posts

Gravity script for scratch scrolling platformer

when green flag clicked
set [y velocity v] to [0]
forever
if <touching [ground sprite v] ?> then
if <(y velocity) < [0]> then
set [y velocity v] to [0]
end
if <key [up arrow v] pressed?> then
set [y velocity v] to [15]
end
else
change [y velocity v] by (-1)
end
change y by (y velocity)
end
Dankiel_
Scratcher
100+ posts

Gravity script for scratch scrolling platformer

hi there, could you please give us more detail or a link to your project?
Thingied
Scratcher
1000+ posts

Gravity script for scratch scrolling platformer

Very basic example:
when green flag clicked
set [Yv v] to [0]
forever
change [Yv v] by (-1) // Gravity
change y by (-1) // Since it's 1 pixel off the ground, move down one to detect the collision
if <<<touching[level v]?>and<(Yv)<[0]>>and<key[jump key v]pressed>> then // If it's touching the ground and the key to jump is pressed, jump
set [Yv v] to (jump power::grey) // Gray blocks are placeholders
end
change y by (1)
change y by (Yv) // Yv is the Y momentum so you have to move whatever it is
if<touching[level v]?> then
get out of level // Running without screen refresh so it does everything instantly
end

define get out of level
repeat until <not<touching[level v]?>>
change y by (()-((Yv)/([abs v] of(Yv)))) // Go the opposite way (the divide part will report 1 if the number is positive then move the other way (-1) because 0 - 1 is -1 and vise versa)
end
set [Yv v] to [0] // Reset the velocity because there's no point in trying to move if you can't

Last edited by Thingied (Aug. 25, 2021 02:00:37)

Emussa
Scratcher
15 posts

Gravity script for scratch scrolling platformer

Hey this is good thanks ill give you credit in the comments

Powered by DjangoBB