Reputation: 11
In my project camera is fixed on player. Enemies are shooting bullets towords player and should be able to still do that while outside screen. However while bullets are created, they won't start moveing until they get on screen.
My code for bullets:
extends Area2D
@export var speed := 200.00
@onready var Ani := $AnimationPlayer
@onready var Me : Callable = Callable(self,"_on_player_position")
@onready var player = get_tree().get_nodes_in_group("Player") #get_node("/root/world/player")
@onready var target_lock=false
@onready var exited_screen_time: float = 0.0
@onready var current_time=0
var time_to_live: float = 3.0
var movement_vector := Vector2.ZERO
var target_vec : Vector2
func _ready():
player=player[0]
look_at(player.position)
target_vec = player.position - self.global_position
movement_vector = target_vec.normalized()
Ani.play("Animation")
func _process(delta):
pass
func _physics_process(delta):
global_position += movement_vector*speed*delta
#func check_lifetime(delta):
func _on_visible_on_screen_enabler_2d_screen_exited():
#pass
queue_free()
#exited_screen_time=current_time
func _on_body_entered(body):
if body.name == 'player':
body.take_damage(5)
if body.can_be_hurt:
queue_free()
The basic idea is to get position of player, at the moment of instance creation, rotate it and move it in this direction.
I tried to use _enter_tree insted of _ready to determine position of player, but this didn't go well since I can't get players node position until it's ready.
How do I get bullets to move the moment they're created, not the moment they're on the screen?
Upvotes: 1
Views: 217
Reputation: 455
I see you've got a VisibleOnScreenEnabler2D
and I just encountered this same problem. On your VisibleOnScreenEnabler2D
under "Enabling" in the inspector, make sure that the Mode is set to "Always".
Upvotes: 0