Shooting a Projectile In Direction Player Is Facing

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

I am new to Godot and Game Development in General and am taking the advice of so many and working on a very small game with a single mechanic. I have a single player character on the screen that rotates within a 180 degree arc, while enemy characters are spawned and fall from the top of the screen. The user can shoot the enemies with a well-timed tap that shoots a projectile.

At the moment I have all of that working and even have the projectiles spawning from the player rotated in the direction they are to travel but am stuck on how I can get them to travel in that direction. I have tried a couple different things and have seen a couple tutorials but the ones I have watched assume you are using a mouse to aim and shoot the projectile towards the location of the mouse. So if anyone knows of any good tutorials or knows the answer help would be appreciated.

Arrow Spawner

extends Node

@export var basic_arrow_scene: PackedScene


func _input(event):
	if event is InputEventScreenTouch && event.is_pressed(): 
		print("Tap Detected - Spawning Arrow...")
		spawn_arrow()
		

func spawn_arrow():
	var player = get_tree().get_first_node_in_group("PlayerGroup") as Node2D
	if player == null:
		return
	
	#instantiate a new arrow object
	var arrow = basic_arrow_scene.instantiate() as CharacterBody2D
	get_parent().add_child(arrow)
	arrow.position = player.global_position
	arrow.rotation = player.rotation
	print("Arrow Position: ", arrow.global_position)
	print("Arrow Direction: ", arrow.global_rotation)

Arrow

extends CharacterBody2D

@export var speed = 400


func _ready():
	#This isn't correct and will shoot the arrow straight up but stripped out my 
	#broken code to make it a bit easier to see where I am currently at
	velocity = Vector2(0,-1)


func _process(delta):
	var collision_info = move_and_collide(velocity.normalized() * delta * speed)
	if collision_info and collision_info.get_collider():
		var enemy = collision_info.get_collider()
		print("Collision Detected with ", enemy)
		remove_arrow()


#The arrow is currently already facing the direction I want it to travel when spawned 
#so is this really necessary
func get_launch_direction():
	var player  = get_tree().get_first_node_in_group("PlayerGroup") as Node2D
	if player == null:
		return


func _on_visibilityNotifier2D_screen_exited():
	remove_arrow()


func on_arrow_body_entered():
	remove_arrow()


func remove_arrow():
	queue_free()
:bust_in_silhouette: Reply From: rockytower

It really is true after wrestling with this for some time it’s like the solution waited until I asked before delivering the answer. Sorry for anyone who might have stumbled on here. At first thought to just hide the question but then figured someone else may stumble here looking for an answer, so going to post what I used to solve my problem.

All i needed was to update my arrow script and update the get_launch_direction()

extends CharacterBody2D

@export var speed = 400
var direction = Vector2(0, -1)

func _ready():
	direction = get_launch_direction()


func _process(delta):
	var collision_info = move_and_collide(direction * delta * speed)
	if collision_info and collision_info.get_collider():
		var enemy = collision_info.get_collider()
		print("Collision Detected with ", enemy)
		remove_arrow()


func get_launch_direction():
	var player  = get_tree().get_first_node_in_group("PlayerGroup") as Node2D
	if player == null:
		return
	else:
		return Vector2(0, -1).rotated(player.global_rotation + TAU)


func _on_visibilityNotifier2D_screen_exited():
	remove_arrow()


func on_arrow_body_entered():
	remove_arrow()


func remove_arrow():
	queue_free()