I want my character to run faster when I press my "E" action.

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

I want my character to run faster when I press my “E” action. I would also like that when I press my action again it would go back to normal speed but I don’t know how to do that.

Heres is the code:

extends KinematicBody

var vertical_velocity = 0
var gravity = 20
var direction = Vector3.FORWARD
var velocity = Vector3.ZERO
var strafe_dir = Vector3.ZERO

var movement_speed = 0
var walk_speed = 3
var run_speed = 12
var acceleration = 7
var angular_acceleration = 7
var super_speed = 50

func _physics_process(delta):
if Input.is_action_pressed(“Forward”) || Input.is_action_pressed(“Backward”) || Input.is_action_pressed(“Left”) || Input.is_action_pressed(“Right”):

	var h_rot = $Camroot/h.global_transform.basis.get_euler().y 
	
	direction = Vector3(Input.get_action_strength("Left") - Input.get_action_strength("Right"),
				0,
				Input.get_action_strength("Forward") - Input.get_action_strength("Backward")).rotated(Vector3.UP,h_rot).normalized()
	
	if Input.is_action_pressed("Sprint"):
		movement_speed = run_speed
		$AnimationTree.set("parameters/iwr_blend/blend_amount", lerp($AnimationTree.get("parameters/iwr_blend/blend_amount"), 1 , delta * acceleration))
	else: 
		$AnimationTree.set("parameters/iwr_blend/blend_amount", lerp($AnimationTree.get("parameters/iwr_blend/blend_amount"), 0 , delta * acceleration))
		movement_speed = walk_speed
else :
	$AnimationTree.set("parameters/iwr_blend/blend_amount", lerp($AnimationTree.get("parameters/iwr_blend/blend_amount"), -1 , delta * acceleration))
	movement_speed = 0
 
if Input.is_action_just_pressed("E"):
	run_speed = super_speed
velocity = lerp(velocity,direction * movement_speed, delta * acceleration)

move_and_slide(velocity + Vector3.DOWN * vertical_velocity,Vector3.UP)

if !is_on_floor():
	vertical_velocity += gravity * delta 
else:
	vertical_velocity = 0
	
$SimplePlayerarma.rotation.y = lerp_angle($SimplePlayerarma.rotation.y,atan2(direction.x,direction.z), delta * angular_acceleration)

Check godot’s tutorial

Vikrant | 2021-06-06 10:37

:bust_in_silhouette: Reply From: Wakatta
if Input.is_action_just_pressed("E"):
    run_speed = super_speed
elif Input.is_action_just_released("E"):
    run_speed = 12

This granted your “E” action is set up in the input editor

Thank you very much but if you don’t mind I would like you to answer another question of mine: If I wanted the run_speed to return to normal only after pressing the “E” for the second time how should I write my script?

Luke776 | 2021-06-07 02:12

if Input.is_action_just_pressed("E"):
    if run_speed == super_speed:
        run_speed = 12
    else:
        run_speed = super_speed

Now I feel like that nerdy kid with the glasses that let you copy off of my test sheet
So who are you in this scenario?

Wakatta | 2021-06-07 02:36

Lol sorry for make you feel that way but thanks your answer is really helpfull

Luke776 | 2021-06-07 03:34

Syke, I joke I joke, I kid I kid. If offend, I’m sorry Please, Please forgive.

Once you get it working.

Wakatta | 2021-06-07 21:01

I know that you was joking dont need to say sorry

Luke776 | 2021-06-08 14:33