How to make sure that the bullet does not chase the player?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zidd

How to make sure that the bullet does not chase the player, but flies to the place where the player was at the time of the shot?

extends Area2D

onready var obj = get_parent().get_node("Player")

var SPEED = 500
var velocity = Vector2()
var target = Vector2()


func _on_VisibilityNotifier2D_screen_exited():
    queue_free()

func _physics_process(delta):
    target = obj.get_global_position()
    velocity = position.direction_to(target) * SPEED * delta
    translate(velocity)

func _on_boss_fire_body_entered(body):
    if "Boss" in body.name:
    	    pass
    else:
	    queue_free()
:bust_in_silhouette: 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?

Zidd | 2020-06-02 09:42

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

Zidd | 2020-06-02 14:26

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

Thank you so much!

Zidd | 2020-06-02 15:48