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)
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.
# 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)