0 votes

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 MAXSPEED = 600
var velocity = Vector2()
var WALK
SPEED = 100

const DIRECTIONLEFT = -1
const DIRECTION
RIGHT = 1
var direction = Vector2(DIRECTION_LEFT, 1)
var ct = true

func setdirection(hordirection):
if hordirection == 0:
hor
direction = DIRECTIONLEFT
var hor
dirmod = hordirection / abs(hordirection)
apply
scale(Vector2(hordirmod * direction.x, 1))
direction = Vector2(hordirmod, direction.y)

func physicsprocess(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 onArea2Dbodyentered(body):
if "Enemy" in body.name:
body.queue_free()

func onTimer_timeout():
ct = true`
Help please!

asked May 29 in Engine by ianzzap (14 points)

1 Answer

+1 vote

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))
answered May 29 by njamster (9,824 points)

Thank you! this works!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.