Discuss Scratch

TheInternetGuy
Scratcher
4 posts

how do you make gravity?

i dont know how to make gravity, please help me!

what i had in mind:
when green flag clicked
forever
if <key [up arrow] pressed?> then
set [y old] to (y position)
repeat (few times)
glide (0.1) secs to x: (x position) y: (my variable)
set [my variable] to (((y old) + ((my variable) + (1))))
end

repeat (few times)
glide (0.1) secs to x: (x position) y: (my variable)
set [my variable] to (((y old) + ((my variable) - (1))))
end

end
end

Last edited by TheInternetGuy (April 14, 2022 08:51:07)

deck26
Scratcher
1000+ posts

how do you make gravity?

The usual method is to have two values - y-velocity and gravity.

Use a loop to control movement and avoid the glide block.

In each pass of the loop change y by y-velocity before changing y-velocity by gravity. So, for example, you jump and set y-veloicty to 8 and gravity is -2. You'd go up 8, then 6, then 4 ,2, 0 before falling with increasing speed -2, -4, -6 and so on. You'll also need to ensure the player doesn't sink into the ground.

The platforumer tutorial by @griffpatch_tutor is a good place to start.
ProfessorUelf
Scratcher
100+ posts

how do you make gravity?

I really recommend you to not use glide. Here's a simple gravity approch:

forever
if <key [up arrow v] pressed?> then
set [y velocity v] to (10)
end
change y by (y velocity)
change [y velocity v] by (-1)
end

Edit: Have a look at what @deck26 posted first!

Last edited by ProfessorUelf (April 14, 2022 09:22:54)

TheInternetGuy
Scratcher
4 posts

how do you make gravity?

thanks
TheInternetGuy
Scratcher
4 posts

how do you make gravity?

anyways fixed your code proffesor uelf
forever
if <<touching [ground/celiing] ?> or <<key [up arrow] pressed?> or <(timer) = [2 or something]>>> then
change [t] by [4 or something]
change y by (t)
end
repeat until <touching [g/c] ?>
change [t] by (-1)
change y by (t)
end
end

Last edited by TheInternetGuy (April 14, 2022 16:21:03)

Powered by DjangoBB