CHARACTER SCRIPT
CHARACTER SCRIPT
Attach a new Script to the Hero node.
Attach a new Script to the Hero node.
Ensure the Template is EMPTY
Ensure the Template is EMPTY
CHARACTER PROPERTIES
CHARACTER PROPERTIES
We have COMPLETE control over the attributes
We have COMPLETE control over the attributes
we can assign to our character.
we can assign to our character.
However the most common properties are for speed,
However the most common properties are for speed,
gravity, jump height etc.
gravity, jump height etc.
Set these up now:
Set these up now:
These are constants which means that their value cannot change while the game is running.
These are constants which means that their value cannot change while the game is running.
However, you can make them standard variables and change the values as you collect powerups etc.
However, you can make them standard variables and change the values as you collect powerups etc.
Set your own gravity var or get it from project settings ensuring all physics bodies use the same gravity.
Set your own gravity var or get it from project settings ensuring all physics bodies use the same gravity.
You can find this value in the project settings menu:
You can find this value in the project settings menu:
GET DIRECTION
GET DIRECTION
In the physics process find out which direction the character is moving in:
In the physics process find out which direction the character is moving in:
APPLY GRAVITY
APPLY GRAVITY
If we are not on the floor,
If we are not on the floor,
gravity should pull us down.
gravity should pull us down.
Note that Y-Axis is NEGATIVE in the Up direction.
Note that Y-Axis is NEGATIVE in the Up direction.
FRAMES PER SECOND
FRAMES PER SECOND
The standard frames per second for Godot can be adjusted here:
The standard frames per second for Godot can be adjusted here:
HANDLE JUMPING
HANDLE JUMPING
If on the floor and we press jump key
If on the floor and we press jump key
We jump at full strength.
We jump at full strength.
Otherwise
Otherwise
If already in air and release jump key
If already in air and release jump key
Halve our Jump strength.
Halve our Jump strength.
HANDLE ACCELERATION
HANDLE ACCELERATION
If we are moving, accelerate up to our max speed
If we are moving, accelerate up to our max speed
Moving FROM current speed, TO max speed at acceleration rate over time
Moving FROM current speed, TO max speed at acceleration rate over time
Note how the move_toward
Note how the move_toward
built-in function works.
built-in function works.
HANDLE DECELERATION
HANDLE DECELERATION
Note that this time we move towards an X velocity of 0 from our current velocity.
Note that this time we move towards an X velocity of 0 from our current velocity.
CALL ALL THE FUNCTIONS!
CALL ALL THE FUNCTIONS!
MOVE AND SLIDE
MOVE AND SLIDE
move_and_slide() is a built-in function which simplifies character movement.
move_and_slide() is a built-in function which simplifies character movement.
What does it do:
What does it do:
Moves the body based on velocity. If the body collides with another, it will slide along the other body (by default only on floor) rather than stop immediately. If the other body is a CharacterBody2D or RigidBody2D, it will also be affected by the motion of the other body. You can use this to make moving and rotating platforms, or to make nodes push other nodes.
Moves the body based on velocity. If the body collides with another, it will slide along the other body (by default only on floor) rather than stop immediately. If the other body is a CharacterBody2D or RigidBody2D, it will also be affected by the motion of the other body. You can use this to make moving and rotating platforms, or to make nodes push other nodes.