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

why doesn't this code work?

extends Area2D
export(int) var speed = 230
export(int) var damage = 15
onready var player = get_parent().get_node("player")
var velocity = Vector2.ZERO
var target_pos: Vector2

func _ready():
    target_pos = player.global_position
    self.rotation = target_pos.angle()
    velocity = self.global_position.direction_to(target_pos)

func _physics_process(delta):
    self.global_position += velocity * speed * delta
Godot version 3.2.3
in Engine by (290 points)

Can you be more specific about "doesn't work?"

first time it shoots towards the player but it keeps shooting in that direction after the first time.

Do you create a new bullet every time or do you just reuse the same one over and over again? Because the ready function which sets the direction the bullet is traveling is only automatically called once.

I create a new one every time.

So you want the bullet to follow the player instead of going in a straight line?

yeah, btw thanks for you precious time.

1 Answer

0 votes
Best answer

The variable target_pos is only set once, under _ready, it's not getting updates with the players position. You need to regularly update the target position with the player's position otherwise the bullet will just go towards the place you set it.

You could try moving the code from _ready() into your _physics_process().

by (276 points)
selected by

thanks, it works now.

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.