Discuss Scratch

Lovecatz647
Teacher
8 posts

How do you make gravity?

Hi! I have been trying to make gravity for my platform game. Can someone help me?
deck26
Scratcher
1000+ posts

How do you make gravity?

Gravity is generally not too hard. Have y-velocity and each time through your main control loop subtract a value for gravity. For example, if you jump and set y-vel to 8 and have gravity as -2 your sprite would go up 8, then 6, then 4, 2, 0, and start falling -2, -4, -6 and so on.
NMario84
Scratcher
1000+ posts

How do you make gravity?

For gravity, simply increase the Y variable over time , and use this variable in your Y speed movement.

I've posted a script in many similar topics like this one. But I'm unsure if it helps.
i_am_tankman
Scratcher
9 posts

How do you make gravity?

when green flag clicked
forever
change y by (-10)
end
helloworldbyeworld
Scratcher
1000+ posts

How do you make gravity?

i_am_tankman wrote:

when green flag clicked
forever
change y by (-10)
end
That might work, but if you do that, it’s not really gravity and it might go through the ground. This is a script that has proper gravity physics and isn’t too complicated:

set [y vel v] to (10) // change this to how much you want to jump (note: it’s not linear and 10 gives you 55px)
repeat until <touching [ground v] ?>
change [y vel v] by (-1)
change y by (y vel)
end
repeat until <not <touching [ground v] ?>> // to prevent going over
change y by (1)
end
bzawodny
Scratcher
12 posts

How do you make gravity?

You should decrease the y value more and more as it jumps, I like to multiply the value by 0.8:

when green flag clicked
set [Y Velocity] to [10]
forever
set [Y Velocity] to ((Y Velocity) * (0.8))
change y by (Y Velocity)
end

Doing the math, the value keeps decreasing little by little.
the6thfirstmunebust
Scratcher
3 posts

How do you make gravity?

well, it's pretty simple. first, make gravity variable. then, we make it so that we decrease the variable at all times.
when green flag clicked
forever
change [gravity] by (-0.1)
end

next, we need to change the Y position with the gravity.

when green flag clicked
forever
change y by (gravity)
end

and, if you ever need it to stop, we must stop the gravity to using this.

set [gravity] to [0]

but, make sure every time you start the game, you have to:

set [gravity] to [0]

BOOM! you have your simple gravity code!

Last edited by the6thfirstmunebust (May 20, 2024 23:35:47)

london11gm
Scratcher
48 posts

How do you make gravity?

I made a project with x and y collisions and gravity. You can use the see inside to see the gravity scripts.
Lightslayr1681
Scratcher
48 posts

How do you make gravity?

forever


change y by (speed y)
change [ speed y] by (-1)


end

Last edited by Lightslayr1681 (May 21, 2024 04:09:17)

23yps04
Scratcher
1 post

How do you make gravity?

You can just do this:

define SetGravity

if <not <touching [ground v] ?>> then
change y by (-5)
end
sebasjames13
Scratcher
1 post

How do you make gravity?

also can
when green flag clicked
forever
change y by (y velocity)



change (y velocity) by (-1)
if <touching ground or edge> then
change (y velocity) by (0)

end
end

Powered by DjangoBB