Hey all, is it possible to exclude values from a variable? I have an archer enemy that is supposed to shoot an arrow although if the player is right above the enemy the arrow logic kinda stops working
extends Area2D
var direction = null
var on_screen = true
@onready var player = get_tree().get_first_node_in_group("Player_body")
@onready var timer: Timer = $VisibleOnScreenNotifier2D/Timer
func _ready(): # gets direction when arrow is instantiated
direction = (player.position - self.global_position).normalized()
if snapped(direction.x,1) == 1:
$Sprite2D.flip_h = false
else:
$Sprite2D.flip_h = true
print(snappedi(direction.x,1))
func _physics_process(delta): # uses direction to flip
position.x += direction.x * 100 * delta
When the arrow instantiates the direction is defined but when the player is above the enemy the arrow direction.x is snapped to 0. this affects the arrow speed in a way that is very inconsistent.
I’ve seen other people deal with a similar issue by making a previous_direction_x variable to handle any direction logic when direction.x == 0
which i dont want it to do. Is it possible to modify the snapped method so as to make it snap to the nearest 1 or -1 instead of 0.
From what I understand of the code, it seems you’re only using the snap functions to change the flip of the arrow. Can’t you just do something like this?
direction = (player.position - self.global_position).normalized()
if direction.x > 0:
$Sprite2D.flip_h = false
else:
$Sprite2D.flip_h = true
No need to snap the x value as you only have to check if it’s positive (looking to the right) or negative (looking to the left).
You can also add a direction.x == 0 case if you need to handle that, but with my code, it will be handled in the else block.
Hope that helps! If not, please attach some screenshots to make the problem more clear
Sorry for the late reply!
The flip script is totally fine. The issue is that when the player is above the enemy the direction.x is defined as 0 which makes the arrow speed really slow. Looking back I should have specified that better and I’m going to edit the post