Newly instantiated arrow follows my Mouse

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

Hello !
I’m trying to understand why my newly created arrow/bullet follows my Mouse instead of the direction I gave in the beginning.

I have a shootingPoint that allways “look_at” my mouse position.
From this position I create an arrow that gets the position and rotation from my shootingPoint.

When I dont move my mouse while the arrow flies it all looks good, but if I move my mouse the direction of the arrow changes. That is not what I want.

Here is my Code:
Bow Script:

extends Area2D

func _ready():
	pass

func _physics_process(_delta):
	look_at(get_global_mouse_position())

func shoot():
	const ARROW = preload("res://Projectiles/arrow.tscn")
	var new_arrow = ARROW.instantiate()
	%ShootingPoint.add_child(new_arrow)
	new_arrow.global_position = %ShootingPoint.global_position
	new_arrow.global_rotation = %ShootingPoint.global_rotation
	new_arrow.rotate(deg_to_rad(90))
	
	

func _on_player_pressed_shoot():
	shoot()

Arrow Script:

extends Area2D

var travelled_distance = 0
const SPEED = 200
const RANGE = 200

func _physics_process(delta):
	var direction = Vector2.RIGHT.rotated(rotation)
	position += direction * SPEED * delta
	travelled_distance += SPEED * delta
	if travelled_distance > RANGE:
		queue_free()

Recording 2024-03-20 at 11.55.22

Your arrow is a child of $ShootingPoint, still parented to your character as it flies. You gotta add it somewhere else in the hierarchy.

1 Like

Thanks for the quick reply.
Any Idea where I can add it without it interacting with anything else? Is there a best practice?

You can make a custom BulletManager as an independent parent node for all bullets/arrows in your scene. That manager can then also deal with cleanup and hit processing. The manager can still be a child of your level node, so all bullets will get deleted on level swap.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.