Problems with shooting mechanism

i’ve been trying to solve the problem for few hours now. I’m trying to make a mechanism that shoots projectiles made out ot particles. I spawn a projectile from a point that rotates around the player but it goes only in one direction no matter rotation.

code attached to a node that moves the points around the player

extends Node2D

@onready var bulletPath = preload("res://Scenes/bullet.tscn")
@onready var bulletmanager = get_node("/root/Node2D/Bulletmanager")
func _ready() -> void:
	pass

func _process(delta: float) -> void:
	if Input.is_action_just_pressed("shoot"):
		shoot()
	
func shoot():
	var bullet = bulletPath.instantiate()
	bullet.global_rotation = $Pivot.global_rotation
	bullet.global_position = $Pivot.global_position
	get_tree().root.get_node("Node2D/Bulletmanager").add_child(bullet)	

bullet code

extends CharacterBody2D
var speed = 500
func _ready() -> void:
	pass

func _process(delta: float) -> void:
	velocity.x = 1 * speed
	move_and_slide()
	

Because your bullet velocity is hardcoded to only go to one side, you need to multiply the speed by 1 or -1 to change the direction. Also, you should use _physics_process instead, everything physics related should be called there.

Would you be able to help me with the code? If I do 1 or -1 it goes right or left, and I need the projectile to go to mouse position

# Bullet code

extends CharacterBody2D
var speed = 500

# Create a variable to store the direction
var direction := Vector.ZERO


func _ready() -> void:
	pass


# Use _physics_process for physics related things
func _physics_process(delta: float) -> void:
	velocity = direction * speed
	move_and_slide()
# Player script
extends Node2D

@onready var bulletPath = preload("res://Scenes/bullet.tscn")
@onready var bulletmanager = get_node("/root/Node2D/Bulletmanager")
func _ready() -> void:
	pass

# Check the player inputs in the _unhandled_input callback instead
func _unhandled_input(event: InputEvent) -> void:
	if Input.is_action_just_pressed("shoot"):
		shoot()
	
func shoot():
	var bullet = bulletPath.instantiate()

	# Use this formula to get the bullet direction
	bullet.direction = (get_global_mouse_position() - global_position).normalized()

	bullet.global_position = $Pivot.global_position
	get_tree().root.get_node("Node2D/Bulletmanager").add_child(bullet)

That worked! Thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.