Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.
My solution i used Vector2.distance_tokazaHello! In my solution i have used the method distance_to:
```gdscript
class_name BulletSpawner
extends Area2D
@export var speed := 800.0
@export var max_range := 2000.0
@export var angle := Vector2(45,45)
@onready var _sprite: Sprite2D = %Sprite
var origin_position: Vector2 = Vector2.ZERO
func _ready() -> void:
origin_position = _sprite.position
func _process(delta: float) -> void:
pass
func _physics_process(delta: float) -> void:
var direction := Vector2.RIGHT.rotated(angle.angle())
_sprite.position += direction * speed * delta
_sprite.rotation = angle.angle()
if _sprite.position.distance_to(origin_position) > max_range:
print("reach max distance...destroying")
_destroy()
func _destroy() -> void:
queue_free()
```
My question is:
I should not use Vector2 function whenever posible? How can I determinate the use or not of this type of functions?
Tks!
20Jan. 28, 2025
My solutionSootyHere is my solution, It seems to work but is a bit different to the code reference. I see you kind of split apart velocity into motion and distance. Is your solution more optimised in the sense of what the computer has to calculate? (also I still can't get the code block to work properly and it crashes my browser)
```gdscript
extends Area2D
@export var max_distance : float = 800.00
@export var speed : float = 3000.00
var velocity : Vector2 = Vector2.ZERO
var _distance_travelled : float = 0.0
func _physics_process(delta: float) -> void:
var direction : Vector2 = Vector2.RIGHT.rotated(rotation)
velocity = direction * speed
position += velocity * delta
_distance_travelled += speed * delta
if _distance_travelled > max_distance:
_destroy()
func _destroy() -> void:
queue_free()
```10Jan. 27, 2025
Utilizing Timer Nodefilthy-rookI've tied a timers wait time function to an exported variable and just use it to call the destroy function, I did this before looking at any of the hints.
Will this cause problems later on, or is this a valid way to handle increasing/decreasing the range?10Jan. 22, 2025
Why _physics_process in Area2D?PurpleSunriseHello,
I thought _physics_process() it's used only for bodies like CharacterBody, StaticBody etc, while we use _process() for areas as areas are not subjected to physics. Or are they? Like areas can't collide right? I thought they just detect other Areas or physic object.
Here's my code:
```gdscript
extends Area2D
@export var speed : float = 100.0
var max_range : float = 300.0
var distance_traveled : float = 0.0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
var direction = transform.x.normalized()
var velocity = direction * speed * delta
position += velocity
distance_traveled += speed * delta
if distance_traveled >= max_range:
destroy()
func destroy()->void:
queue_free()
```
Thank you!70Jan. 21, 2025
Is it ok to calculate the bullet distance using position?Lucas Pscheidt```gdscript
func _ready() -> void:
initial_position = global_position
func _process(delta: float) -> void:
if max_range < global_position.distance_to(initial_position):
_destroy()
```10Jan. 02, 2025
Lesson Q&A
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.