Hey, im trying to make a boomerang projectile that comes back to the player after 1 second or after the projectile collides with something. Ive managed to make the player shoot the boomerang, and i've made the boomerang spin, but i cant figure out how to make it come back to the player and dissapear. Heres my code:
Player code:

	if Input.is_action_just_pressed("action") and $Timer.is_stopped(): shoot()
func shoot():
	var inst: Projectile = projectile.instantiate()
	owner.add_child(inst)
	inst.transform = $SpawnP.global_transform
	$Timer.start()

Boomerang code:

extends Node2D
class_name Projectile
var speed: float = 5.0 
var target = null
u/onready var player = get_node("res://UnScene/player.tscn")

func _ready():
	$Timer.start()

func _physics_process(delta):
	position += transform.x * speed
	$Sprite2D.rotation += 0.3
	
	if $Timer.is_stopped():
		pass
		#position.move_toward(player.position, delta*speed)
  • The big question is here do you want the boomerang to be physics based or are you wanting the movement to be far more consistent than that? If you want the boomerang to just fly towards a target zelda style I would actually have a simple invisible 3D node with the boomerang mesh a child of that and have the parent node rotate. It would take a bit of fiddly setting up but it's a simple technique that should work, other solutions involve potentially taking the distance and angle between the player and the target and doing some fancy lerping.

The big question is here do you want the boomerang to be physics based or are you wanting the movement to be far more consistent than that? If you want the boomerang to just fly towards a target zelda style I would actually have a simple invisible 3D node with the boomerang mesh a child of that and have the parent node rotate. It would take a bit of fiddly setting up but it's a simple technique that should work, other solutions involve potentially taking the distance and angle between the player and the target and doing some fancy lerping.

  • tmk replied to this.

    Lethn i don't need it to be physics based. I've manged to make it shoot out of the plater and rotate. All i need now is for the boomerang to travel back to the player after x number of seconds or x number of tiles traveled. I also want the boomerang to go back to the player if it hits something. While the boomerang is in the air the player should be unable to throw another boomerang. Do you know a way to make it travel back to the player?

    Lethn when i try to reference the players position i get a null. Idk why. I think this is the problem. The boomerang and player aren't in a scene together, could this be the problem? The bullet is a projectile in the CharacterBody2D.

    Yes, you need to declare the player within the scene during runtime in order to get it's position for this to work, the principle is the same as when you're declaring your projectile though, there are multiple methods to look up on how to get a node during runtime if you're adding it into the scene when the scene loads rather than having it already there among other things, that will be something for you to study up. It's important to understand the debug messages as well because doing this quickly help you identify what the engine is telling you, null almost always means that the engine doesn't think there's something there, so that means something went wrong in runtime or you didn't grab the node you need to for the code to work correctly.

    • tmk replied to this.

      Lethn i cant figure out how to refrence the player tho. I've tried get_node, NodePath and export. Nothing works. Do you know what code i should use? Could you come with an example?

      We need to see your scene tree. Place this in Boomerang's _ready() function, and post the output here:
      get_tree().get_root().print_tree()

      • tmk replied to this.

        DaveTheCoder i've now managed to refrence the player using a global script. Now the only problem is that the boomerang comes back to the player, but stops before it collides with it. But if i turn up the boomerang speed it comes closer and closer. The speed is now at 5, but if i turn it up to 30 it comes all the way back to the player, but this is way to fast. If i turn it up to 100 it travels past the player and then comes back once again.
        Heres the code for the returning boomerang:
        position = position.lerp(Global.player.global_position,delta*backspeed)

        what should i change to fix this?

        • tmk replied to this.

          tmk the boomerang is an Area2d node, so i cant use move_and_collide or move_and_slide

            I don't know. You're the boomerang expert here.

            Maybe you need to write an algorithm to manually control the position, instead of using lerp().

            tmk Incorrect, you can make the Area2D node a child of a character or other node that uses kinematic movement.