Godot Version
4.3Question
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()