if you want to do a dash, i would rather set the speed for a given time instead of than a given distance... is esentially the same as speed*time = distance. Do you have any project right now to which you wish to add the dash? if so, you may share it so i can see how could dash be added to your project.
Basically i would first declare a var dashing = false
at the beggining of character script. I would also add a Timer called DashTimer
as child of character, with autostart disabled and one shot enabled. When you press the action for dash, i would set dashing = true
and call $DashTimer.start()
. I would connect timeout singal of the timer to on_DashTimer_timeout()
function, and there i would set dashing = false
. In _physics_process
i would use a match or nested if to if dashing == true
. In that case, instead of handling movement like always, i would set speed to 1000 and the correct direction for the dash. I would define motion as the speed rotated to the direction. Then just handle de movement with motion = move_and_slide(motion, floor_normal)
. When timer timeouts, dashing will be set to false, and so normal movement can be done.
You can also omit the timer, and had a variable that you add with delta on each time you enter _physics_process
with dashing = true
.
However, this are only thoughts.. if you share me the project maybe i can help further.
Edit
This is the code i modified from extra info given in comments below:
const UP = Vector2(0,-1)
const GRAVITY = 9.8
const MAX_SPEED = 200
const ACELERATION = 10
const JUMP_HIGH = -400
var DASH = 800
var DASH_DISTANCE = 100.0 #must be a float for the division!
var movement = Vector2()
# dash
var dash_direccion = 1
var dash_time = DASH_DISTANCE/DASH
var dash_timer = dash_time
var canWalk = true
var canDash = true
var inDash = false
func _physics_process(delta):
movement.y += GRAVITY
if inDash:
dash_timer += delta
if dash_timer > dash_time:
inDash = false
if dash_timer < dash_time:
inDash = true
if not inDash:
if Input.is_action_pressed("ui_left"):
if canWalk:
movement.x = max(movement.x - ACELERATION, -MAX_SPEED)
dash_direccion = -1
elif Input.is_action_pressed("ui_right"):
if canWalk:
movement.x = min(movement.x + ACELERATION, MAX_SPEED)
dash_direccion = 1
else:
movement.x = 0
if Input.is_action_just_pressed("ui_dash"):
if canDash:
movement.x = DASH * dash_direccion
dash_timer = 0
canDash = false
inDash = true
if is_on_floor() and not inDash:
canDash = true
else:
canDash = false
movement = move_and_slide(movement, UP)