How to make bullet go into direction towards cross hair/screen center

Godot Version

v4.6.1.stable.fedora [14d19694e]

Question

Hey, i am trying to make shooting stuff in my game, one problem i am facing is how can i get a direction which is towards the center of the screen where my crosshair is. Bullets travel far and are fast so using Ray cast didn’t help. i am a beginner

Here is input script part when i press shoot

	if Input.is_action_pressed("shoot") and shoot:
		shoot = false
		var b = bullet.instantiate()
		get_tree().current_scene.add_child(b)
		b.global_position = nozzle.global_position
		# b.direction = what to write here????
		await get_tree().create_timer(Global.BULLET_TIMEOUT).timeout
		shoot = true

and here is bullet script

extends Area3D

@export var direction : Vector3
var speed = Global.BULLET_SPEED

func _physics_process(delta: float) → void:
if not direction : return
global_position += direction * speed * delta

func _on_timer_timeout() → void:
queue_free()

func _on_body_entered(body: Node3D) → void:
body.queue_free()
print(“Target Eliminated”)
queue_free()

“Center of the screen” is not a single point in 3D space. It projects to infinite number of points lying along the viewing direction ray. So project that ray, check what it hits and send the bullet there.

1 Like

but what if there is no wall. my game environment is space

Then take a point on that ray that’s very far, but no need to go farther than the camera far plane.