I want to use Shift to increase the speed of the player

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

I started using godot 3 days ago and I’ve write the following code for the player:

extends KinematicBody2D

const SPEED = 200

var movedir = Vector2(0,0)

func _physics_process(delta):
    controls_loop()
    movement_loop()

    if Input.is_action_pressed("ui_left"):
	    $AnimationPlayer.play("Walk Left")
    elif Input.is_action_pressed("ui_right"):
	    $AnimationPlayer.play("Walk Right")
    elif Input.is_action_pressed("ui_up"):
	    $AnimationPlayer.play("Walk Up")
    elif Input.is_action_pressed("ui_down"):
	    $AnimationPlayer.play("Walk Down")
    else:
	    $AnimationPlayer.play("Idle")

func controls_loop():
    var LEFT	= Input.is_action_pressed("ui_left")
    var RIGHT	= Input.is_action_pressed("ui_right")
    var UP	= Input.is_action_pressed("ui_up")
    var DOWN	= Input.is_action_pressed("ui_down")

    movedir.x = -int(LEFT) + int(RIGHT)
    movedir.y = -int(UP) + int(DOWN)


func movement_loop():
    var motion = movedir.normalized() * SPEED
    move_and_slide(motion, Vector2(0,0))

What I want to do is that when I press SHIFT, the player speed doubles or multiplies it by 1.5. Anyone know how to do this?

:bust_in_silhouette: Reply From: mokalux

hello there,

I did it this way:

func hero_walk(xbool:bool):
	if xbool:
		move_speed = 5 * 32
	else:
		move_speed = 10 * 32

then:

func _input(event):
	if event.is_action_pressed("ui_accept"):
		hero_walk(false)

	if event.is_action_released("ui_accept"):
		hero_walk(true)

I think you can adapt it to your code!

I’ve adapted it to my code and it worked! Thanks!

Unknowledge | 2019-05-19 20:07

great, thanks for the accepted answer. have fun!

mokalux | 2019-05-19 21:54

:bust_in_silhouette: Reply From: felipefacundes

Just declare the SPEED variable without defining it, and it will be toggled when pressing SHIFT:

var SPEED;
const WALK_SPEED = 200;
const RUN_SPEED = 300;

func _physics_process(delta):
  SPEED = WALK_SPEED;
if Input.is_action_pressed("run"):
  SPEED = RUN_SPEED;
else:
  SPEED = WALK_SPEED;

See:

https://godotforums.org/discussion/26553/2d-platformer-make-the-player-run-when-a-key-is-pressed-while-moving

Thank you so much you helped me a lot :-).

nasim_mhmmd | 2022-10-28 16:49