KinematicBody2D - how to move it fast a short distance

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nahsch

Hi. Thanks.

Hi, you should really add more explanation. Or an example game that has what you want to implement. You want to do something like a dash? why not using move_and_slide or move_and_collide?

p7f | 2019-01-16 15:41

For example: I want to move my kinematicBody2D at 1000 speed but only 100 distance

var myVector1 = Vector2 (1000,0)
var myVector2 = Vector2 (100,0)

If…

move_and_slide (myVector1) # (1000,0) too much distance
move_and_slide (myVector2) # distance wanted but too slow

move_and_collide is not what I want becouse has no slide

¿Is possible to increase/regulate movement speed? ¿How?

nahsch | 2019-01-17 00:58

Hi, can you convert this to a comment? Answers are only for the moment you solved the issue…

I’ll do some tests but i think you just need to store the initial position when movement started. Do the movement with Vector2(1000,0) and stop the movement when distance to initial point is higher thatn 100. Dou you want to trigger this kind of movement with some key? so i can test further… or maybe you can even share the project.

p7f | 2019-01-17 12:31

Ok. Thanks for comment. Im very new to this so sorry xD

I want to do a dash when press a Key.
I want dash speed (be?) more than run speed

Stop movement when distance reach 100 like you say sound good. How can I do that?

nahsch | 2019-01-17 13:34

:bust_in_silhouette: 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.

p7f | 2019-01-17 15:29

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?

p7f | 2019-01-17 15:32

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.

p7f | 2019-01-17 16:13

Ah, i also had to add “ui_dash” action, because it was not added in the input map

p7f | 2019-01-17 16:15

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…

p7f | 2019-01-17 16:19

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.

p7f | 2019-01-18 02:00