Automatic directions for bullets

Godot Version

4.6.2

Question

i’m using a character 2d node to make a bullet type of entity, this is the code i’m using (excluding variable declarations), but for some reason when direction is 0 it works just fine, same thing when it’s -1 but when it’s 1 it justs becomes invisible

func despawn():
	if dead == false:
		animated_sprite_2d.play("splash")
		dead = true
		if animated_sprite_2d.is_playing():
			return
		queue_free()


func _physics_process(delta: float) -> void:
	if not is_on_floor():
		velocity += get_gravity() * delta * 1.5
#sistema right
	if direction >= 0:
		if not ray_r.is_colliding():
			velocity.x = Speed * delta * 1
		else:
			despawn()
#left
	elif direction < 0:
		if not ray_l.is_colliding():
			velocity.x = Speed * delta * -1
		else:
			despawn()

Put print statements in your if block to see code execution path.

hey nvm, i figured it out, i needed the bullet to spawn with a little bit more x position that the character, still don’t know why since the character can’t collide with the rays, but it’s not a problem with me holding the key to walk, but it’s the character’s going forward

Hey so a little update, it wasn’t the character’s going forward, ite the character colliding with the rays

It was a dukb assumption of me, while I disabled the rays 1 mask for the L I didn’t do it for the L

This should be handled using collision layers and masks so that collision between the player and their bullets is never registered by the engine.

Yeah, i didn’t do that with the R but i did it with the L, i just forgot