Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | ianzzap |
I have tried everything to execute this correctly, but my caveman brain has not advanced enough yet. I have tried the “if AnimationPlayer.is_playing()” function, but changed to a timer only to find that I am still having the same problem. Whenever I try to move forward while attacking, my direction gets reversed. Here is the code: `extends KinematicBody2D
const MAX_SPEED = 600
var velocity = Vector2()
var WALK_SPEED = 100
const DIRECTION_LEFT = -1
const DIRECTION_RIGHT = 1
var direction = Vector2(DIRECTION_LEFT, 1)
var ct = true
func set_direction(hor_direction):
if hor_direction == 0:
hor_direction = DIRECTION_LEFT
var hor_dir_mod = hor_direction / abs(hor_direction)
apply_scale(Vector2(hor_dir_mod * direction.x, 1))
direction = Vector2(hor_dir_mod, direction.y)
func _physics_process(delta):
if Input.is_action_pressed("ui_left"):
velocity.x -= WALK_SPEED
if velocity.x < -MAX_SPEED:
velocity.x = -MAX_SPEED
if ct == true:
set_direction(DIRECTION_LEFT)
elif ct == false:
set_direction(DIRECTION_RIGHT)
elif Input.is_action_pressed("ui_right"):
velocity.x += WALK_SPEED
if velocity.x > MAX_SPEED:
velocity.x = MAX_SPEED
if ct == true:
set_direction(DIRECTION_RIGHT)
elif ct == false:
set_direction(DIRECTION_LEFT)
else:
velocity.x = lerp(velocity.x, 0, 0.2)
if Input.is_action_pressed("ui_up"):
if velocity.y < -MAX_SPEED:
velocity.y = -MAX_SPEED
velocity.y -= WALK_SPEED
elif Input.is_action_pressed("ui_down"):
velocity.y += WALK_SPEED
if velocity.y > MAX_SPEED:
velocity.y = MAX_SPEED
else:
velocity.y = lerp(velocity.y, 0, 0.2)
if Input.is_action_just_pressed("KILL"):
$AnimationPlayer.play("atak")
$Timer.start()
ct = false
velocity = move_and_slide(velocity, Vector2(0, -1))
func _on_Area2D_body_entered(body):
if “Enemy” in body.name:
body.queue_free()
func _on_Timer_timeout():
ct = true`
Help please!