Bullets can't track position of player

Godot Version

4.6

Question

Trying to make an enemy shoot towards the player, but for some reason the bullets only shoot towards the spawn point of the player and do not track.

tracking and bullet spawn code


var direction_to_player = (player.global_position-global_position).normalized()
var bb1 = big_bullet_scene.instantiate()
bb1.position = self.position
bb1.dir = direction_to_player
get_parent().add_child(bb1)

bullet code

var dir = Vector2(0, 0)

func _ready() -> void:
	pass 

func _process(delta: float) -> void:
	self.position += dir * delta * 100

video of the problem

Why would you expect them to? You point them in one direction and send them on their way.

That’s because your bullet is pointing to Vector2.ZERO. You need it to point to the players position on shoot not zero.

Can you share more of your first script? Especially how player is defined and in what function the snippet is running.

It looks like direction_to_player could be set once at the game start such as in _ready rather than every time the bullet needs to spawn, but then your code sample would have to be merge from multiple sources rather than what your game is really running.

What does the player scene look like and how do you move the player? Specifically, which node in the player scene moves?

Its root seems to be a Node2D. If you only move some child node in that scene, the root node’s position won’t change.

I use this for a tracking projectile:

func _physics_process(delta: float) -> void:
	if not target == null:
		look_at(target.global_position)
	position += transform.x * speed * delta

target is set to the node I am shooting at.

this was it! my script was on a characterbody2d as a child of the player Node2d, so i retargeted and it works now, THANK YOU SO MUCH!