Always idle facing left?

I think I might have screwed up and odds are it is something simple. I am fairly new to code but have been watching videos and reading tutorials. With this in mind I think something is off as my walk animations are working but my idle animations is only ever in one direction. If left is there it is always left regardless to order I place them in. If I remove left then it switches to another. They all work but I have to disable the rest. Any help is appreciated.

extends KinematicBody2D

export var speed = 200

export var motion_speed = 200

const idle_speed = 10

var raynode

var PlayerAnimNode
var anim = ""
var animnew = ""

onready var sprite = get_node("sprite")

var vel = Vector2()

func _ready():
set_fixed_process(true)
set_pos(Vector2(500, 300))
raynode = get_node("RayCast2D")
PlayerAnimNode = get_node("sprite")

func _fixed_process(delta):
var motion = Vector2()

    if (Input.is_action_pressed("ui_right")):
        motion += Vector2(1, 0)
        raynode.set_rotd(90)

    if (Input.is_action_pressed("ui_left")):
        motion += Vector2(-1, 0)
    raynode.set_rotd(270)

    if (Input.is_action_pressed("ui_down")):
        motion += Vector2(0, 1)
        raynode.set_rotd(0)

    if (Input.is_action_pressed("ui_up")):
        motion += Vector2(0, -1)
        raynode.set_rotd(180)

    motion = motion.normalized()*motion_speed*delta
    move(motion)

# set animation

    if (motion.length() > idle_speed*0.09):
        if (Input.is_action_pressed("ui_up")):
            anim = "walk_up"
        if (Input.is_action_pressed("ui_down")):
            anim = "walk_down"
        if (Input.is_action_pressed("ui_left")):
            anim = "walk_left"
        if (Input.is_action_pressed("ui_right")):
            anim = "walk_right"
    else:
        if (raynode.get_rotd() == 180):
            anim = "idle_up"

        if (raynode.get_rotd() == 0):
            anim = "idle_down"

        if (raynode.get_rotd() == 270):
            anim = "idle_left"

        if (raynode.get_rotd() == 90):
            anim = "idle_right"

    if anim != animnew:
        animnew = anim
        PlayerAnimNode.play(anim)

Answers

  • _807__807_ Posts: 57Member
    Answer ✓

    Try to not compare float numbers with == inside the "else". Do that with >= an add "and" to ever conditon to limit range. Ej: if raycast.rotd() >= x and raycast.rotd() <= y: anim= ..........
    It fix problem?

  • _807__807_ Posts: 57Member
    edited September 2017

    You have bad identation in line 8? It miss one TAB?
    If it is out the scope of the if, it always do rotd=270 aka always left.

  • SageMoonbladeSageMoonblade Posts: 2Member

    Thank you for the help, that first one was just the trick.

Sign In or Register to comment.