I no longer believe in gravity

I know this is a common question across the forum, but I am having a rough time trying to create gravity for my game. (please help)

1 Like

Here’s one way to make gravity work:

Step 1:

Create a variable called gravity.
Go to the default script and type this:

project.gravity=0;

The sets the gravity to zero by default.

Step 2:

Gravity goes down, so be it.
Go inside of the update script, and write this:

project.gravity-=2;

This makes the gravity go down by 2, feel free to change the gravity force by changing this number.

Step 3:

Who falls with gravity?
Go to the update script of the clip that you want to fall with gravity, and type this in the update script:

this.y-=project.gravity

This makes the object fall with gravity.

Step 4:

Know when to reject or reflect gravity
Make a new clip and name it “ground”
Go back to the player clip and add this to the update script:

if(this.hitTest(ground)){
if(isKeyDown('up')){
project.gravity=15;
}else{
project.gravity=0;
this.y=ground.y-((ground.height/2)+(this.height/2)-1);
}
}

I didn’t check this code, but it should take the player to the top of the ground platform and make them jump if the up key is pressed down and the player is touching the ground

Step 5:

Add movement keys, and play around with the code a bit to have it fit your project.
To make the player move, add this inside of the update script for the player:

if(isKeyDown("right"))this.x+=14;
if(isKeyDown("left"))this.x-=14;

This is a simple method to add gravity to your project, let me know if there’s any typos I made or errors that come up. Anyone feel free to edit this post if you want to explain or add a step

Thanks.

2 Likes

Thank You so much!

1 Like