Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | kanaszm |
Hello,
So I’ve made this KinematicBody2D 8-directional movement in GDScript, and I would like to hear feedback and if I can optimize it better. Currently, the controlled animated sprite can walk, run and dodge. I used lerp to add a sense of weight to the character and a little slide when dodging.
extends KinematicBody2D
var direction = Vector2(0,0)
var lerping_speed = Vector2(0,0)
var current_speed = Vector2(0,0)
var running_speed = Vector2(500, 500)
var walking_speed = Vector2(250, 250)
var entity
func _ready():
entity = $Player
set_process(true)
set_process_input(true)
func _input(Input):
if Input.is_action_pressed("ui_shift_left"):
$Player.play("left_run")
current_speed = running_speed
direction.x = -current_speed.x
elif Input.is_action_pressed("ui_ctrl_left"):
if $DodgeCD.is_stopped():
$DodgeCD.start()
direction.x = -800
lerping_speed.x = lerp(lerping_speed.x, direction.x, 1)
$Player.play("left_dash")
elif Input.is_action_pressed("ui_left"):
$Player.play("left")
current_speed = walking_speed
direction.x = -current_speed.x
elif Input.is_action_released("ui_left"):
direction.x = 0
if $AnimWait.is_stopped():
$AnimWait.start()
$Player.play("center")
if Input.is_action_pressed("ui_shift_right"):
$Player.play("right_run")
current_speed = running_speed
direction.x = current_speed.x
elif Input.is_action_pressed("ui_ctrl_right"):
if $DodgeCD.is_stopped():
$DodgeCD.start()
direction.x = 800
lerping_speed.x = lerp(lerping_speed.x, direction.x, 1)
$Player.play("right_dash")
elif Input.is_action_pressed("ui_right"):
$Player.play("right")
current_speed = walking_speed
direction.x = current_speed.x
elif Input.is_action_released("ui_right"):
direction.x = 0
if $AnimWait.is_stopped():
$AnimWait.start()
$Player.play("center")
if Input.is_action_pressed("ui_shift_up"):
$Player.play("up_run")
current_speed = running_speed
direction.y = -current_speed.y
elif Input.is_action_pressed("ui_ctrl_up"):
if $DodgeCD.is_stopped():
$DodgeCD.start()
direction.y = -800
lerping_speed.y = lerp(lerping_speed.y, direction.y, 1)
$Player.play("up_dash")
elif Input.is_action_pressed("ui_up"):
$Player.play("up")
current_speed = walking_speed
direction.y = -current_speed.y
elif Input.is_action_released("ui_up"):
direction.y = 0
if $AnimWait.is_stopped():
$AnimWait.start()
$Player.play("center")
if Input.is_action_pressed("ui_shift_down"):
$Player.play("down_run")
current_speed = running_speed
direction.y = current_speed.y
elif Input.is_action_pressed("ui_ctrl_down"):
if $DodgeCD.is_stopped():
$DodgeCD.start()
direction.y = 800
lerping_speed.y = lerp(lerping_speed.y, direction.y, 1)
$Player.play("down_dash")
elif Input.is_action_pressed("ui_down"):
$Player.play("down")
current_speed = walking_speed
direction.y = current_speed.y
elif Input.is_action_released("ui_down"):
direction.y = 0
if $AnimWait.is_stopped():
$AnimWait.start()
$Player.play("center")
func _process(delta):
lerping_speed.x = lerp(lerping_speed.x, direction.x, 0.09)
lerping_speed.y = lerp(lerping_speed.y, direction.y, 0.09)
entity.set_position(entity.get_position() + lerping_speed * delta)