Player pos not updating in bullet node

Godot Version

Godot 4

Question

I have a player node and a bullet node. im trying to get the bulle tto point towards the player i have the following code in the bullet,

extends CharacterBody2D

@onready var player = get_parent().get_parent().get_node('playerNode')
# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	look_at(player.position)
	print(player.position)

when the game starts it points towards the player but doesn’t update when i move the player. i found that this is because the player position isn’t updated to the bullet even though it IS in the player. so why wont it update the player position?


when i print(position) inside player it gives accurate
when i print(player.position) in bullet it gives the position it started at

use global_position

that didn’t work still does everything the same

delete “@onready”?

when i do that it just says “cannot call method ‘get_parent’ on a null value”

try replace “get_node(…)” to get_child(0) and delete @onready.
leave one line var player
and in the ready script specify player = (as it is now, but with my corrections)

extends CharacterBody2D

var player
# Called when the node enters the scene tree for the first time.
func _ready():
	player = get_parent().get_parent().get_child(0)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	look_at(player.global_position)
	print(player.global_position)

i changed it to this but it still does the same exact thing as when i started the post

if all this doesn’t work, you can do the following:
create a script and don’t attach it to a node.
For example, name it GlobalScript.gd, then plug it into the autoloader in the project settings. ’
Add the PlayerPos variable to it
Then in the player’s script in PhysicsProcess write the line:
GlobalScript.PlayerPos = global_position.
After in the bullet script in PhysicsProcess write.
look_at(GlobalScript.PlayerPos)
print(GlobalScript.PlayerPos)

Wait, I think I figured out what the problem is.

I figured it out, turns out i think i was referencing the parent node but not the charecter2d node so i referenced the charecter 2d node and that fixed it

extends CharacterBody2D

@onready var player = get_parent().get_parent().get_node('playerNode').get_child(0)
# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	look_at(player.position)
	print(player.position)

1 Like

Your character itself is a CharacterBody2D, which is attached to Node2D, that is, in addition, you just need to write after get_child(0) another get_child(index node CharacterBody from top to bottom).

Yeah, while I was writing in the translator, you already figured it out.

thankyou for the help kind stranger

1 Like

you’re welcome

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