Bullet jitters when not hitting player

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

Hello, I’m new to programming and Godot.
I need help with my bullet, if it hits the player it disappears “queue_free()”, acts normal.
However, if my player moves the missed bullet gets stuck and it jitters.

Code:

extends Area2D

var speed = 300

onready var player = get_node(“/root/Node/Player”)
var target = Vector2()
var velocity = Vector2()

func _ready():
look_at(position)
target = player.global_position

func _physics_process(delta):
velocity = position.direction_to(target) * speed
velocity = velocity.rotated(rotation)

translate(velocity * delta)

func _on_VisibilityNotifier2D_screen_exited():
queue_free()

func _on_Redlaser_body_entered(body):
queue_free()

maybe try

func physicsprocess(delta):
        if target  != null:
                velocity = position.direction_to(target) * speed
          


func onVisibilityNotifier2Dscreenexited():
     target = null
     queue_free

ramazan | 2022-02-10 13:11

:bust_in_silhouette: Reply From: Inces

You set target in ready() and never change it. So bullet moves to the position when player was at the moment of bullets birth. It jitters because as it passes this position it comes back to it endlessly. You have to update the target in process() if You want homing missile behavior, or move setting the velocity to ready() if You want normal bullet behavior