Godot Version
4.3
Question
I have a boomerang type projectile that flies a max distance before returning. I want the process to take a similar amount of time no matter how far the projectile is thrown.
Here is the current behavior:
Here is the code where the boomerang object is immediately in motion as soon as it is instantiated.
class_name WeaponThrow extends Node2D
enum State {
INACTIVE,
THROW,
RETURN
}
var player : Player
var direction: Vector2
var speed: float
var state
var destroy
var distance : int = 250
var throwLocation
@export var acceleration: float = 400.0
@export var max_speed: float = 400.0
func _ready() -> void:
visible = false
state = State.INACTIVE
player = PlayerManager.player
func _physics_process(delta: float) -> void:
if state == State.INACTIVE:
var mousePos = get_global_mouse_position() #direction of dash
var rotationF = atan2(mousePos.y-position.y,mousePos.x-position.x) #rotation of line between player and mouse
throwLocation = Vector2(distance*cos(rotationF)+position.x,distance*sin(rotationF)+position.y) #calculation of the point to where should be dashed
if position.distance_to(mousePos) < distance:
throwLocation = mousePos
state = State.THROW
if state == State.THROW:
rotation += .3
speed -= acceleration * delta
position = position.move_toward(throwLocation, speed * delta)
if position.distance_to(throwLocation) <= 10 or speed <= 0:
state = State.RETURN
elif state == State.RETURN:
rotation += .3
speed += acceleration * delta
position = position.move_toward(player.position, speed * delta)
if position.distance_to(player.position) <= 10 or speed <= 0:
queue_free()
func throw( throw_direction : Vector2) -> void:
direction = throw_direction
speed = max_speed
visible = true
func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
destroy = true
await get_tree().create_timer(2).timeout
if destroy:
queue_free()
func _on_visible_on_screen_notifier_2d_screen_entered() -> void:
destroy = false