0 votes

(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

Godot version 3.5
in Engine by (286 points)

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

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.