Discuss Scratch

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?

whenclickedsetsv_gravityto0wait10secssetsv_gravityto1ifsv_gravity=1thenaddgravitytocheatsifcheatscontainsgravity?thensayThis is gravity cheat

Last edited by ReadHowToMakeTheGame (May 22, 2024 16:31:46)

GvYoutube
Scratcher
1000+ posts

How to make a gravity script?

ReadHowToMakeTheGame wrote:

I need to make the game with jump but I don't know how to make it
whenup arrowkeypressedchangeyby10changeyby-10

Last edited by GvYoutube (May 22, 2024 16:26:17)

ReadHowToMakeTheGame
Scratcher
11 posts

How to make a gravity script?

But with gravity script
born_2010
Scratcher
5 posts

How to make a gravity script?

ReadHowToMakeTheGame wrote:

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:
whenclickedset y velocityto0foreverchange y velocityby-1changeybyyvelocity
This 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.
I hope this helps!
--CHA0S--
Scratcher
100+ posts

How to make a gravity script?

born_2010 wrote:

ReadHowToMakeTheGame wrote:

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:
whenclickedset y velocityto0foreverchange y velocityby-1changeybyyvelocity
This 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.
I hope this helps!
Then adding on to this, add into the script after the change y block
iftouchingground?thenchangeybyyvelocity*0
This is the core of the collision if you need it
Lightslayr1681
Scratcher
48 posts

How to make a gravity script?

For the jump, I recommend in the “if touching ground”, you put,
ifkey up arrowpressed?thenchange Y velocityby10

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.



whenclickedsetyvelocityto0foreverchangeyvelocityby-1ifcanJump?=1thensetyvelocityto10 You can change this number to make the jump height higher or lowerchangeybyyvelocityiftouchingground?thenGroundCollisionselsesetcanJump?to0defineGroundCollisionsrepeatuntilnottouchingground?changeyby-1*yvelocity/absofyvelocityifyvelocity<0thensetcanJump?to1elsesetcanJump?to0 If the player was coming out of a ground collision, set canJump to 1.setyvelocityto0

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?

whenclickedsetGravityto1foreverchangeGravityby1changeyby0-Gravity
This is the script I use for gravity
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.
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?

Create-Scratch101 wrote:

There is a good wiki article: Simulating Gravity
That 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?

ReadHowToMakeTheGame wrote:

whenclickedsetsv_gravityto0wait10secssetsv_gravityto1ifsv_gravity=1thenaddgravitytocheatsifcheatscontainsgravity?thensayThis is gravity cheat
Why are you asking for help if you literally just typed the code out?
ReadHowToMakeTheGame
Scratcher
11 posts

How to make a gravity script?

HogwartsTeacherSnape wrote:

ReadHowToMakeTheGame wrote:

Rickrolled,yea
ItBeJC
Scratcher
1000+ posts

How to make a gravity script?

tank401 wrote:

Love how you come into a forum to say someone is wrong, ignoring the guy who wants help

ItBeJC wrote:

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.



whenclickedsetyvelocityto0foreverchangeyvelocityby-1ifcanJump?=1thensetyvelocityto10 You can change this number to make the jump height higher or lowerchangeybyyvelocityiftouchingground?thenGroundCollisionselsesetcanJump?to0defineGroundCollisionsrepeatuntilnottouchingground?changeyby-1*yvelocity/absofyvelocityifyvelocity<0thensetcanJump?to1elsesetcanJump?to0 If the player was coming out of a ground collision, set canJump to 1.setyvelocityto0

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?

ReadHowToMakeTheGame wrote:

I need to make the game with jump but I don't know how to make it
whenclickedsety_vel whatever you want to name it is fineto0foreverchangeybyy_velwhateveryouwanttonameitisfinechangey_vel whatever you want to name it is fineby0ifkeyspacepressed?thensety_vel whatever you want to name it is fineto12
Sub2Newtticus
New Scratcher
1 post

How to make a gravity script?

ok



























































































































































ReadHowToMakeTheGame
Scratcher
11 posts

How to make a gravity script?

Thanks for help everyone _B)_

Powered by DjangoBB