To create a dash we will use Timers and duplicate a node
To create a dash we will use Timers and duplicate a node
We will split dashing into two functions - the dash and the effect
We will split dashing into two functions - the dash and the effect
In the Player Scene add three new timers as children of the CharacterBody2D node
In the Player Scene add three new timers as children of the CharacterBody2D node
Name them DashDurationTimer and DashEffectTimer and DashCoolDownTimer
Name them DashDurationTimer and DashEffectTimer and DashCoolDownTimer
Set wait time for DashDurationTime to 0.4
Set wait time for DashDurationTime to 0.4
Set timer to One Shot
Set timer to One Shot
Set wait time for DashEffectTimer to 0.1
Set wait time for DashEffectTimer to 0.1
Set wait time for DashCoolDownTimer to 2
Set wait time for DashCoolDownTimer to 2
Set the timer to One Shot
Set the timer to One Shot
In the Player Script add three new global variables:
In the Player Script add three new global variables:
is_dashing, can_dash and dash_speed
is_dashing, can_dash and dash_speed
We also need access to the timers
We also need access to the timers
add reference variables for both of them.
add reference variables for both of them.
Dash function
Dash function
We need a custom action event linked to a key for dashing.
We need a custom action event linked to a key for dashing.
To create this:
To create this:
Add a new Input Action for the dash Click on:
Add a new Input Action for the dash Click on:
Project --> Project Settings --> Input Map
Project --> Project Settings --> Input Map
Where it says: Add New Action, type in dash
Where it says: Add New Action, type in dash
Then press Enter
Then press Enter
Click on the + and the system will wait for an input to register with the action word dash.
Click on the + and the system will wait for an input to register with the action word dash.
Press a key to associate with dash - I have used E
Press a key to associate with dash - I have used E
Code the dash function:
Code the dash function:
If the player presses the dash key and dash is not in cool down
If the player presses the dash key and dash is not in cool down
set is_dashing to true
set is_dashing to true
put dash in cool down
put dash in cool down
start the dash timer
start the dash timer
start the dash effect timer.
start the dash effect timer.
start the dash cool down timer.
start the dash cool down timer.
If dashing increase speed by dash speed
If dashing increase speed by dash speed
Do not dash upwards
Do not dash upwards
Call the dash function in process
Call the dash function in process
Connect the timeout signal of the DashDurationTimer and code it:
Connect the timeout signal of the DashDurationTimer and code it:
Connect the timeout signal of the
Connect the timeout signal of the
DashCoolDownTimer and code it:
DashCoolDownTimer and code it:
The Cool Down Timer prevents the player from being able to spam the dash key and essentially fly.
The Cool Down Timer prevents the player from being able to spam the dash key and essentially fly.
The player can now dash!
The player can now dash!