Discuss Scratch

Kosi_101O
Scratcher
50 posts

How Do You Use Gravity?

Wondering how to use gravity in my projects. I keep trying to and I need help.
The_Cool_Scratch_Guy
Scratcher
100+ posts

How Do You Use Gravity?

The main way to create a gravity effect is for objects to accelerate downwards.

First, we need to go downwards.

define move vertically at (speed)
repeat until <some condition>
change y by (speed)

A positive speed will go up and a negative speed will go down. This will have you gliding until you meet some condition, perhaps until you are touching the floor. However, gliding at a steady speed is weird. Real gravity increases the speed as you are falling.

define fall (speed) (gravity)
set [speed v] to (speed)
set [acceleration v] to ((-1) * (gravity)) // Because gravity is downwards, this makes it so you don't have to enter a negative number.
repeat until <some condition>
change [speed v] by (acceleration)
change y by (speed)

This will have you falling more naturally. Notice that speed is not made negative, because sometimes it can make sense to have an upward speed. If you are jumping, for example, it might make sense to put 20 for speed and 1.5 for gravity. For the most part, speed controls how fast you go up and gravity controls how fast you come back down, and you can play around with your numbers until you find something that works well.
Kosi_101O
Scratcher
50 posts

How Do You Use Gravity?

That helps a lot, thanks
Whimsical_Dandellion
Scratcher
12 posts

How Do You Use Gravity?

there is another way, you could…

when green flag clicked
set [gravity v] to [-0.5]
set [y velocity v] to [0]
forever
change [y velocity v] by (gravity)
change y by (y velocity)
end

in this way, gravity is sort of “set as a constant” so for every frame, the y velocity variable will go down by 0.5, and then we just set the change y by y velocity which makes it go down. im an amateur and this is the easiest way i found. and u need to code the floor collision otherwise your sprite is just going to go down forever. also dont forget the y velocity variable needs to be set to “for this sprite only” and the gravity too. and if u want ur character to jump (like in a platformer) u need to play around with an x velocity variable. idk
10goto10
Scratcher
1000+ posts

How Do You Use Gravity?

Gravity should make the object fall faster and faster. A Scratch team member made this project to compare the two methods. https://scratch.mit.edu/projects/964424785/

That same Scratch team member also made this gravity example project: https://scratch.mit.edu/projects/964424785/
Kosi_101O
Scratcher
50 posts

How Do You Use Gravity?

Yeah, ok I'm good now bro

Powered by DjangoBB