This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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()
in Engine by (17 points)

1 Answer

0 votes

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.

by (22,191 points)

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) 

That will still change direction every frame, which is not what OP wanted.

Sure? The target variable is only assigned in the first cycle of the loop.

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.

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()

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

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)

Thank you so much!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.