Bullet script not working!

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

Whenever i shoot a bullet it goes in its intended direction. But when i shoot a second bullet it makes the first bullet change direction. How do i fix this ? Also the screen notifier is not removing the bullet i.e it’s not printing “bullet removed”

extends CharacterBody2D

var can_shoot = false
const RIGHT = Vector2.RIGHT
@export var SPEED: int = 8500

@onready var Marker = get_tree().get_root().find_child("Marker", true, false)
@onready var Gun = get_tree().get_root().find_child("Gun", true, false)

func _ready():

    global_position = Marker.global_position 

func _physics_process(delta):

    if Input.is_action_just_released("ui_left_click"):
	    can_shoot = true

    if can_shoot == true:
	    var directiion = Gun.rotation
	    var movement = RIGHT.rotated(directiion) * SPEED * delta
	    global_position += movement
func _on_VisibilityNotifier2D_screen_exited():
    print("Bullet Removed")
:bust_in_silhouette: Reply From: evpevdev

Shooting a second bullet makes the first bullet change direction because your bullets keep checking for left clicks even after they were shot. To fix this you can put the code for changing the direction and movement in the _ready function instead of the _physics_process function

var direction
var movement
func _ready():
    global_position = Marker.global_position 
    direction = Gun.rotation
    movement = RIGHT.rotated(direction) * SPEED * delta

func _physics_process(delta):
    global_position += movement

The screen notifier may be not working because you have not put a CollisionShape2D as a child of your bullet, or because you are not using the move_and_slide( ) function to move your bullet.

This was helpful thx

javrocks | 2023-04-20 13:51