shoot spear in the right direction

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cire_arievilo1

(Google translator) Hi, I’m new here and this is my first time asking something here, anyway, how do I make my spear go in the direction of my aim?

My code

Player.gd

extends Node2D

var state = "idle"

onready var animation: AnimationPlayer = $AnimationPlayer
onready var spear_inst = preload("res://Scenes/Spear.tscn")

func _input(event: InputEvent) -> void:
if event.is_action_pressed("attack") and state == "idle":
	state = "attack"
	create_spear()

func create_spear():
var spear = spear_inst.instance()
spear.position = $AimPosition.global_position
spear.direction = $TargetPosition.global_position
spear.rotation_degrees = $AimPosition.rotation_degrees
get_parent().add_child(spear)

func _on_AnimationPlayer_animation_finished(anim_name: String) -> void:
if anim_name == "Attack":
	animation.play("Idle")
	state = "idle"

Spear.gd

extends Area2D
var direction: Vector2 = Vector2.ZERO
var speed = 50

func _physics_process(delta: float) -> void: 
    translate(direction.normalized() * speed * delta)

func  _on_VisibilityNotifier2D_screen_exited() -> void: 
    queue_free()

gif 1

gif 2

gif 3

and nobody answered… and this whole time and I still haven’t solved the problem, my project is stopped because of it.

Cire_arievilo1 | 2022-12-18 03:12