Discuss Scratch
- Discussion Forums
 - » Help with Scripts
 - » How to make a gravity script?
        
         
- ReadHowToMakeTheGame
 - 
                            
						
						
                            Scratcher
                        
						
						 
11 posts
How to make a gravity script?
I need to make the game with jump but I don't know how to make it
                        
                        
                    - ReadHowToMakeTheGame
 - 
                            
						
						
                            Scratcher
                        
						
						 
11 posts
How to make a gravity script?
when green flag clicked
set [sv_gravity] to [0]
wait (10) secs
set [sv_gravity] to [1]
if <[sv_gravity] = [1]> then
add [gravity] to [cheats]
end
if <[cheats] contains [gravity] ?> then
say [This is gravity cheat]
end
Last edited by ReadHowToMakeTheGame (May 22, 2024 16:31:46)
- GvYoutube
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
How to make a gravity script?
I need to make the game with jump but I don't know how to make it
when [up arrow v] key pressed
change y by (10)
change y by (-10)
Last edited by GvYoutube (May 22, 2024 16:26:17)
- born_2010
 - 
                            
						
						
                            Scratcher
                        
						
						 
5 posts
How to make a gravity script?
I need to make the game with jump but I don't know how to make it
Although I'm not sure if this is what you need, this is how I usually create gravity scripts in Scratch:
First, I start with a variable called “y velocity,” and set it to 0 at the beginning.
After that, I make a forever loop, and inside it I place a ‘change y velocity by -1’ block.
Right below that block I place a ‘change y by y velocity’ block.
The script should look similar to this:
when green flag clickedThis is the core of the program, as without it you cannot have realistic gravity. After you make this, you may wish to add a component that makes the player collide with the ground when they land on it. If so y velocity should be set to 0 when they touch the ground so that they stop falling. Something else you might want to consider is jumping. If you want to make the player jump, you should probably just set y velocity to a positive number like 15 when they press the jump key. This will make them move up instead of down. If you want to make them fall faster or slower, change the “-1” in the script to something larger or smaller. Either way, there are multiple different things this script can be used for that involve gravity, so this should probably be useful no matter what you are working on.
set [ y velocity] to [0]
forever
change [ y velocity] by (-1)
change y by (y velocity)
end
I hope this helps!

- --CHA0S--
 - 
                            
						
						
                            Scratcher
                        
						
						 
100+ posts
How to make a gravity script?
Then adding on to this, add into the script after the change y blockI need to make the game with jump but I don't know how to make it
Although I'm not sure if this is what you need, this is how I usually create gravity scripts in Scratch:
First, I start with a variable called “y velocity,” and set it to 0 at the beginning.
After that, I make a forever loop, and inside it I place a ‘change y velocity by -1’ block.
Right below that block I place a ‘change y by y velocity’ block.
The script should look similar to this:when green flag clickedThis is the core of the program, as without it you cannot have realistic gravity. After you make this, you may wish to add a component that makes the player collide with the ground when they land on it. If so y velocity should be set to 0 when they touch the ground so that they stop falling. Something else you might want to consider is jumping. If you want to make the player jump, you should probably just set y velocity to a positive number like 15 when they press the jump key. This will make them move up instead of down. If you want to make them fall faster or slower, change the “-1” in the script to something larger or smaller. Either way, there are multiple different things this script can be used for that involve gravity, so this should probably be useful no matter what you are working on.
set [ y velocity] to [0]
forever
change [ y velocity] by (-1)
change y by (y velocity)
end
I hope this helps!
if <touching [ground v] ?> thenThis is the core of the collision if you need it
change y by ((y velocity) * (0))
end

- Lightslayr1681
 - 
                            
						
						
                            Scratcher
                        
						
						 
48 posts
How to make a gravity script?
For the jump, I recommend in the “if touching ground”, you put,
Since gravity is constantly pulling the player, this should work
                        
                            if <key [ up arrow] pressed?> then
change [ Y velocity] by (10)
end
Since gravity is constantly pulling the player, this should work
Last edited by Lightslayr1681 (May 22, 2024 21:16:56)
- ItBeJC
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
How to make a gravity script?
Everybody here has given part of a fully functional jumping script, but not the full script.
(Currently editing this post to include scripts)
EDIT: Forgot to include ceiling collisions. Its included now.
Keep in mind these scripts can be optimized but they should work for your needs. Also make sure to tick “Run Without Screen Refresh” on the custom block or it will not work correctly.
                        
                            (Currently editing this post to include scripts)
EDIT: Forgot to include ceiling collisions. Its included now.
when green flag clicked
set [yvelocity v] to [0]
forever
change [yvelocity v] by (-1)
if <(canJump?) = [1]> then
set [yvelocity v] to [10] // You can change this number to make the jump height higher or lower
end
change y by (yvelocity)
if <touching [ground v] ?> then
Ground Collisions :: custom
else
set [canJump? v] to [0]
end
end
define Ground Collisions
repeat until <not <touching [ground v] ?>>
change y by ((-1) * ((yvelocity) / ([abs v] of (yvelocity))))
end
if <(yvelocity) < [0]> then // If the player was coming out of a ground collision, set canJump to 1.
set [canJump? v] to [1]
else
set [canJump? v] to [0]
end
set [yvelocity v] to [0]
Keep in mind these scripts can be optimized but they should work for your needs. Also make sure to tick “Run Without Screen Refresh” on the custom block or it will not work correctly.
Last edited by ItBeJC (May 23, 2024 19:58:03)
- Blazing_Codes
 - 
                            
						
						
                            Scratcher
                        
						
						 
37 posts
How to make a gravity script?
when green flag clickedThis is the script I use for gravity
set [Gravity v] to [1]
forever
change [Gravity v] by (1)
change y by ((0) - (Gravity))
end
- tank401
 - 
                            
						
						
                            Scratcher
                        
						
						 
100+ posts
How to make a gravity script?
For actual gravity you need a bunch of things, Gravity, velocity, and Y value
Gravity is set in stone and never changes.
Velocity goes down by the value of Gravity when you're not touching the ground.
Y values goes down by the value of velocity.
This should simulate acceleration, you need to do the touching things though.
                        
                        
                    Gravity is set in stone and never changes.
Velocity goes down by the value of Gravity when you're not touching the ground.
Y values goes down by the value of velocity.
This should simulate acceleration, you need to do the touching things though.
- Create-Scratch101
 - 
                            
						
						
                            Scratcher
                        
						
						 
500+ posts
How to make a gravity script?
There is a good wiki article: Simulating Gravity
                        
                        
                    - ItBeJC
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
How to make a gravity script?
There is a good wiki article: Simulating GravityThat deals with gravity around a center point, not platformer gravity, which is what they're asking for.
- tank401
 - 
                            
						
						
                            Scratcher
                        
						
						 
100+ posts
How to make a gravity script?
Love how you come into a forum to say someone is wrong, ignoring the guy who wants help
                        
                        
                    - HogwartsTeacherSnape
 - 
                            
						
						
                            Scratcher
                        
						
						 
100+ posts
How to make a gravity script?
Why are you asking for help if you literally just typed the code out?when green flag clicked
set [sv_gravity] to [0]
wait (10) secs
set [sv_gravity] to [1]
if <[sv_gravity] = [1]> then
add [gravity] to [cheats]
end
if <[cheats] contains [gravity] ?> then
say [This is gravity cheat]
end
- ItBeJC
 - 
                            
						
						
                            Scratcher
                        
						
						 
1000+ posts
How to make a gravity script?
Love how you come into a forum to say someone is wrong, ignoring the guy who wants help
Everybody here has given part of a fully functional jumping script, but not the full script.
(Currently editing this post to include scripts)
EDIT: Forgot to include ceiling collisions. Its included now.when green flag clicked
set [yvelocity v] to [0]
forever
change [yvelocity v] by (-1)
if <(canJump?) = [1]> then
set [yvelocity v] to [10] // You can change this number to make the jump height higher or lower
end
change y by (yvelocity)
if <touching [ground v] ?> then
Ground Collisions :: custom
else
set [canJump? v] to [0]
end
end
define Ground Collisions
repeat until <not <touching [ground v] ?>>
change y by ((-1) * ((yvelocity) / ([abs v] of (yvelocity))))
end
if <(yvelocity) < [0]> then // If the player was coming out of a ground collision, set canJump to 1.
set [canJump? v] to [1]
else
set [canJump? v] to [0]
end
set [yvelocity v] to [0]
Keep in mind these scripts can be optimized but they should work for your needs. Also make sure to tick “Run Without Screen Refresh” on the custom block or it will not work correctly.
- maxc1ne
 - 
                            
						
						
                            Scratcher
                        
						
						 
1 post
How to make a gravity script?
 .
                        
                            Last edited by maxc1ne (Feb. 6, 2025 18:04:19)
- codeisBOOM
 - 
                            
						
						
                            Scratcher
                        
						
						 
100+ posts
How to make a gravity script?
I need to make the game with jump but I don't know how to make it
when green flag clicked
set [y_vel whatever you want to name it is fine v] to [0]
forever
change y by (y_vel whatever you want to name it is fine)
change [y_vel whatever you want to name it is fine v] by (0)
if <key [space v] pressed?> then
set [y_vel whatever you want to name it is fine v] to [12]
end
end
- Discussion Forums
 - » Help with Scripts
 - 
            » How to make a gravity script? 
         












