|
|
|
|
Reply From: |
p7f |
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)
# dash
var dash_direccion = 1
var dash_time = 0.5
var dash_timer = 0.5
var canWalk = true
var canDash = true
var inDash = false
func _physics_process(delta):
movement.y += GRAVITY
dash_timer += delta
if dash_timer > dash_time:
inDash = false
if dash_timer < 0.5:
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
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
if inDash:
pass
else:
movement = move_and_slide(movement, UP)
pass
that is my code so far. I dont know how to share project right now
nahsch | 2019-01-17 15:27
for sharing the project you can upload to google drive, mega, or any cloud storage service and paste the link here.
I’ll look at it when i have time and see what i can do.
Looking it fast, it looks Ok to except for one thing… why you pass movement while inDash?
if inDash:
pass
else:
movement = move_and_slide(movement, UP)
pass
I think you should always compute movement, shouldn’t you?
Link to project MEGA
nahsch | 2019-01-17 15:58
That part is where you are helping me
if inDash:
#do dash thing
nahsch | 2019-01-17 15:59
Just leave move_and_slide
in boths cases…
This works razonably well in my pc (or at least i like it):
extends KinematicBody2D
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)
with this code, you can just adjust DASH
variable for the dash speed, and that won’t affect dash distance, or DASH_DISTANCE
for modifying the distance without changing the speed.
Ah, i also had to add “ui_dash” action, because it was not added in the input map
Also notice that i used inDash
to only increas dash_timer
when dashing. I don’t thing adding delta will cause an overflow before you cal dash again, but just in case…
Very thanks for your time and effort p7f. Yeah work fine that way
nahsch | 2019-01-18 01:58
Glad to help! i’ll edit my answer to add the code… please, if it worked select the answer so others can quickly find the solution.