How to Check The Direction A Kinematic2D is Facing

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Alocktrix

Hello I’m having trouble on using my [var is_facing], in an [if] statement. The [func facing] works but for some reason when I use it as a check for my [func dash], the [if] statement doesn’t go through. And yes [func dash], and [func facing], is in [func _physics_process].

My apologies if my english/terminology is off.

 var is_facing = Vector2.ZERO
        
   func facing():
    	if Input.is_action_pressed("Left"):
    		var is_facing = Vector2.LEFT
    #		print(is_facing)
    
    	elif Input.is_action_pressed("Right"):
    		var is_facing = Vector2.RIGHT
    #		print(is_facing)
    
    func dash():
    	if Input.is_action_just_pressed("Dash") and is_facing == Vector2.LEFT:
    		velocity.x = velocity.x - dash_lenth
    		print("dash left")
    
    	elif Input.is_action_just_pressed("Dash") and is_facing == Vector2.RIGHT:
    		velocity.x = velocity.x + dash_lenth
    		print("dash right")
:bust_in_silhouette: Reply From: jgodfrey

Assuming that code you haven’t shown is working as you describe, my first guess is that you’re getting bit by a floating point error that’s causing your vector equality checks to fail (== Vector2.LEFTfor example).

If that is indeed the problem changing that to something like this might fix the issue…

if Input.is_action_just_pressed("Dash") and is_facing.is_equal_approx(Vector2.LEFT)

That’ll return true if the compared vectors are very close to being equal, with requiring them to be exactly equal…