Attach a new Script to your Player node
Attach a new Script to your Player node
DELETE the generated code and leave only the first line.
DELETE the generated code and leave only the first line.
Set up your variables
Set up your variables
Use the function PHYSICS PROCESS. This checks code every frame at a consistent rate.
Use the function PHYSICS PROCESS. This checks code every frame at a consistent rate.
Apply gravity by checking if the player is NOT on the floor.
Apply gravity by checking if the player is NOT on the floor.
Then change vertical velocity by our gravity variable.
Then change vertical velocity by our gravity variable.
MULTIPLYING BY DELTA CONVERTS A per frame ACTION INTO A per second ACTION.
MULTIPLYING BY DELTA CONVERTS A per frame ACTION INTO A per second ACTION.
Setup horizontal movement
Setup horizontal movement
Here we set the horizontal velocity to 0.
Here we set the horizontal velocity to 0.
Then we change x negatively when we want to move left
Then we change x negatively when we want to move left
We change x positively when we want to move right.
We change x positively when we want to move right.
FInally, apply the velocity using the move_and_slide function.
FInally, apply the velocity using the move_and_slide function.
This function allows the player to slide up and down ramps and not get stuck.
This function allows the player to slide up and down ramps and not get stuck.
Save the script and return to the scene view. The game has no camera so we must add one.
Save the script and return to the scene view. The game has no camera so we must add one.
Make the Camera a child of the Player so that it follows the Player
Make the Camera a child of the Player so that it follows the Player
Zoom in a bit by changing the zoom property
Zoom in a bit by changing the zoom property
Click on Add Element
Click on Add Element
To collide with the ground we must change the properties of the TileMap
To collide with the ground we must change the properties of the TileMap
In the Tiles tab, choose TileSet, Select Paint Tool and switch to the Physics Layer 0 we just added.
In the Tiles tab, choose TileSet, Select Paint Tool and switch to the Physics Layer 0 we just added.
Now paint all of the tiles which you want to be solid with the left mouse button.
Now paint all of the tiles which you want to be solid with the left mouse button.
Testing the Game
Testing the Game
- The playe can now move left and right using the arrow keys.
- Gravity will affect the Player if it falls off the edge of the terrain.
- The player can collide with solid objects.
Add jumping
Add jumping