Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do you make gravity?
- Lovecatz647
-
8 posts
How do you make gravity?
Hi! I have been trying to make gravity for my platform game. Can someone help me?
- deck26
-
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
-
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've posted a script in many similar topics like this one. But I'm unsure if it helps.
- i_am_tankman
-
9 posts
How do you make gravity?
when green flag clicked
forever
change y by (-10)
end
- helloworldbyeworld
-
1000+ posts
How do you make gravity?
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:when green flag clicked
forever
change y by (-10)
end
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
-
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:
Doing the math, the value keeps decreasing little by little.
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
-
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.
next, we need to change the Y position with the gravity.
and, if you ever need it to stop, we must stop the gravity to using this.
but, make sure every time you start the game, you have to:
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
-
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
-
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
-
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
-
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
- Discussion Forums
- » Help with Scripts
-
» How do you make gravity?