Sure thing.
To use a timer, declare a float variable, which will be used to count the dash duration in seconds:
var dash_timer: float = 0
Next, add a is_dashing
variable, to know if player is currently dashing:
var dash_timer: float = 0
var is_dashing: bool = false
Next, when triggering the dash (wherever you do that, could be for instance in _input
or _process
), set the variables like this:
is_dashing = true
dash_timer = 1
1
being here the dash duration in seconds, so, change this value to whatever is good for your game’s dash mechanic, it doesn’t have to be 1.
Now that is_dashing
is set to true, you have to set it back to false, otherwise the dash will be infinite. To do that, use dash_timer
to know how much seconds remain: if the value is >= 0, that means there is some time left, else, that means the dash is over.
If there’s time left, you need to reduce the value over time, otherwise it will always be >= 0.
In _process(delta)
, you have a parameter called delta
that represents the time spent since the last frame. Here’s a link to a more in depth explanation, that’s okay is you don’t get all of the concepts here.
What you need to understand if that, by decrementing a value by delta
, you decrement it over time, in seconds, which makes the value act like a timer:
func _process(delta):
dash_timer -= delta
The last thing we’re missing here is checking if the timer has reached 0, to reset is_dashing
to false:
func _process(delta):
dash_timer -= delta
if dash_timer < 0:
is_dashing = false
Then, just use the is_dashing
value (true or false) to know if the character is dashing or not, and apply it to anything (collision, speed, particles, whatever you need).
The full code would look something like this:
var dash_timer: float = 0
var is_dashing: bool = false
func _process(delta):
if Input.is_action_just_pressed("dash"):
is_dashing = true
dash_timer = 1
dash_timer -= delta
if dash_timer < 0:
is_dashing = false
Let me know if that helps.