|
|
|
 |
Reply From: |
kidscancode |
You’re changing the direction of the bullet every frame by using direction_to()
in _physics_process()
. Don’t do this, just move the bullet in a straight line. Set the direction once when the bullet first spawns.
Declaring the variable as var target = Vector2 () godot takes it as null, so I think this should work:
func _physics_process(delta):
if !target:
target = obj.get_global_position()
velocity = position.direction_to(target) * SPEED * delta
translate(velocity)
estebanmolca | 2020-05-30 18:57
That will still change direction every frame, which is not what OP wanted.
kidscancode | 2020-05-31 17:40
Sure? The target variable is only assigned in the first cycle of the loop.
estebanmolca | 2020-05-31 20:19
I see - I read it as you getting the target node each frame. Note obj.global_position
is preferred. getters are not needed for node properties.
The next problem with this will be that the bullet will turn around when it goes past the target position. The direction should be set at _ready()
time when the bullet spawns. After that, it just travels in a straight line.
kidscancode | 2020-05-31 20:29
i agree, it is better to use ready:
func _ready():
target= obj.position
func _physics_process(delta):
velocity = position.direction_to(target) * SPEED * delta
translate(velocity)
if position.distance_to(target) < 5:
queue_free()
estebanmolca | 2020-05-31 21:48
Thank you. And how to make sure that the bullet continued to fly along the trajectory after it reached the target?
When you multiply a vector by a scalar you stretch or shrink the vector without changing its direction, so you can multiply obj.position by a fairly large value as if it were off-screen, or better yet, you can normalize it (make its module count one without changing its direction) and create a distance variable for the multiplication:
var distance= 1500
func _ready():
target= obj.position.normalized()*distance
estebanmolca | 2020-06-02 10:16
In this case, the bullet just flies left
Sorry…try this:
func _ready():
target= obj.position
velocity = position.direction_to(target) * SPEED
func _physics_process(delta):
translate(velocity*delta)
estebanmolca | 2020-06-02 15:02