How do I keep my player from turning the opposite direction while an animation is playing? (2D)

Attention Topic was automatically imported from the old Question2Answer platform.

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`

This works for me:

``````extends KinematicBody2D

var velocity = Vector2()
const WALK_SPEED = 100
const MAX_SPEED = 600

const DIRECTION_LEFT = -1
const DIRECTION_RIGHT = 1
var direction =  Vector2(DIRECTION_LEFT, 1)

func set_direction(hor_direction = DIRECTION_LEFT):
if not \$AnimationPlayer.is_playing():
apply_scale(Vector2(hor_direction * direction.x, 1))
direction = Vector2(hor_direction, direction.y)

func _physics_process(delta):
if Input.is_action_pressed("ui_left"):
velocity.x = max(velocity.x-WALK_SPEED, -MAX_SPEED)
set_direction(DIRECTION_LEFT)
elif Input.is_action_pressed("ui_right"):
velocity.x = min(velocity.x+WALK_SPEED, MAX_SPEED)
set_direction(DIRECTION_RIGHT)
else:
velocity.x = lerp(velocity.x, 0, 0.2)

if Input.is_action_pressed("ui_up"):
velocity.y = max(velocity.y-WALK_SPEED, -MAX_SPEED)
elif Input.is_action_pressed("ui_down"):
velocity.y = min(velocity.y+WALK_SPEED, MAX_SPEED)
else:
velocity.y = lerp(velocity.y, 0, 0.2)

if Input.is_action_just_pressed("ui_accept"):
\$AnimationPlayer.play("attack")

velocity = move_and_slide(velocity, Vector2(0, -1))
``````

Thank you! this works!

ianzzap | 2020-05-29 20:27