Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to make a gravity script?
- war_rmine
-
Scratcher
1 post
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
- garnled
-
Scratcher
3 posts
How to make a gravity script?
when green flag clicked
forever
set [gravity v] to []
end
- donut6262
-
Scratcher
1 post
How to make a gravity script?
i need help my grond collision is not working
- Discussion Forums
- » Help with Scripts
-
» How to make a gravity script?






