Projectiles not firing straight from an angle

Godot Version

4.6.1

Question

I’ve got a system in my game that involves a projectile that fires outward from a pivot point that can be rotated left and right. The projectile goes until it’s gone off screen or hit something then returns back to the spawn point. There are upgrades that can increase the number of projectiles fired at once. I’m having trouble getting the projectile to fire straight from the angle that it was at. It often times moves off and to the right or left depending on what code I’m trying. There’s a good bit of code here so I don’t want to oversaturate the post with code. I will say that my projectile’s position is (0,0)’d out and faces the right. There is a hitbox for it that only covers the tip. This is some of the projectile’s code:
func set_dir(dir:Vector2):
directionn = dir.normalized()

func _ready():
base_position = global_position
speed *= PlayerUpgrade.get_spear_speed_mult()func set_dir(dir:Vector2):
directionn = dir.normalized()

func _ready():
base_position = global_position
speed *= PlayerUpgrade.get_spear_speed_mult()

func _process(delta):
if state == “outward”:
global_position += directionn * speed * delta
elif state == “stuck”:
timer -= delta
if timer <= 0:
state = “returning”
elif state == “returning”:
var direction = (base_position - global_position).normalized()
position += direction * return_speed * delta
if global_position.distance_to(base_position) < 10:
if hooked_fish:
var fish_item = send_to_convert(hooked_fish)
send_to_inv(fish_item)
projectile_finished.emit()
queue_free()

Hopefully my formatting worked well.
here’s some of the shooter’s script:
func spawn_spread(count:int, spread:float):
active_projectiles = count
for i in range(count):
var projectile = projectile_scene.instantiate()
var t = 0.0
if count > 1:
t = float(i) / float(count - 1)
var angle_offset = lerp(-spread, spread, t)
var rot = spawn_point.global_rotation + angle_offset
projectile.global_position = spawn_point.global_position
projectile.set_dir(Vector2.RIGHT.rotated(rot))
projectile.projectile_finished.connect(_on_projectile_finished)
get_tree().current_scene.add_child(projectile)

func shoot():
var spear_count = PlayerUpgrade.get_multi_spear_count()
spear_sprite.visible = false
projectile_active = true
spawn_spread(spear_count, deg_to_rad(20))

I’ll be straightforward and say that I’m not extremely experienced. Some of this code I wrote, some I tried following other people’s code, and some I borrowed from an LLM. I’ve been on this code for too long I can’t see it right anymore and I think I’ve let it get muddled. Any help would be greatly appreciated. If you need more information from me, I am happy to supply.

It didn’t unfortunately. You should put your code between tripple backticks ``` on both sides.

```gdscript
<your code here>
```
1 Like

Look at your positioning code to determine if you are mixing local and global coordinates.