Why can i not find the player position?

Godot Version

4.3

Question

im trying to make a boomerang weapon that moves towards the mouse and then returns to the player. I dont know why but it doesnt work and just disappears when it is supposed to return to the player. Help please. here are the scripts.
Boomerang code:
class_name Boomerang extends Node2D

enum State { INACTIVE, THROW, RETURN }

var direction : Vector2
var speed_b : float = -200
var state
var bomerang_direction : Vector2
var initial_position : Vector2
var target_position : Vector2
var player_p : Node2D = get_node_or_null(“.”)

@export var acceleration : float = 200

func _ready():
state = State.THROW
initial_position = position
target_position = get_global_mouse_position()
look_at(target_position)
if player_p == null:
print(“not found”)
return

func _process(delta):
if state == State.INACTIVE:
look_at(get_global_mouse_position())

func _physics_process(delta: float):
if state == State.THROW:
speed_b += acceleration * delta
position += bomerang_direction * speed_b * delta
if speed_b >= 0:
state = State.RETURN
elif state == State.RETURN:

	if player_p != null:
		var current_player_position = player_p.position
		speed_b += acceleration * delta
		position = position.move_toward(current_player_position,speed_b * delta )
		if global_position.distance_to( current_player_position ) < 5:
			queue_free()
			pass

and this is the players script:
extends CharacterBody2D

@onready var BOMERANG_scene = preload(“res://sceens/boomerang.tscn”)
const SPEED = 100.0

func _process(delta):
if Input.is_action_just_pressed(“fire”):
shoot()

func _physics_process(delta):
var direction_x = Input.get_axis(“move_left”, “move_right”)
var direction_y = Input.get_axis(“move_up”, “move_down”)
if direction_x or direction_y:
velocity.x = direction_x * SPEED
velocity.y = direction_y * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.y = move_toward(velocity.y, 0, SPEED)

move_and_slide()

func shoot():
var bomerang = BOMERANG_scene.instantiate()
bomerang.position = position
get_parent().add_child(bomerang)
print(“boom”)
bomerang.bomerang_direction = (position -get_global_mouse_position()).normalized()

This should be:

@onready var player_p : Node2D = get_node_or_null("..")

You are currently setting player_p to the boomerang itself, so when it reaches the RETURN state it is checking if the distance from itself is less than 5, so it queue_free()s itself immediately. Additionally, this needs to be an @onready variable, otherwise it won’t have access to other nodes in the tree. This defers the initialization until after the scene is in the tree and can access other nodes in the tree.

1 Like

now it travels to the players original position (when he entered the sceane)

Oops, my bad, I forgot that the boomerang wasn’t being added as a child of the player. Instead of getting the player in the boomerang node, you should set the player_p variable in the player script after you set the position:

func shoot():
	var bomerang = BOMERANG_scene.instantiate()
	bomerang.position = position
	bomerang.player_p = self
	get_parent().add_child(bomerang)
	print("boom")
	bomerang.bomerang_direction = (position -get_global_mouse_position()).normalized()

Then in the boomerang script:

var player_p : Node2D

!thank you

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