Weird Marker2D behavior with barebones code

Godot Version

Godot 4.2.1


Hi, I’m new to programming in Godot but I’ve been playing around with things I’ve learned in various tutorials. That is, until I ran into the following issue which I can’t seem to debug. I’m attaching a video to show what’s happening.

When I first run it, the child bullets work really well with the position at the Gun’s Marker2D, direction and rotation according to the code. Once I move the player though, the global_position of the Marker2D seems to translate in the same Vector as the player movement but greatly increased. So rotation and direction still work fine, but the starting position is way off. It’s even possible to move back to the original pos and then the bullets start at the right place again.

Code at

extends CharacterBody2D

const SPEED = 400

var bullet_scene: PackedScene = preload("res://projectile.tscn")

func _process(_delta):
    var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
    velocity = direction * SPEED

    if Input.is_action_just_pressed("primary_shot"):
        var weapon_rotation = (get_global_mouse_position() - position).normalized()
        var shot_pos = $Gun/Marker2D.global_position
        var other_rotation = get_angle_to($Gun/Marker2D.global_position)
        #print("shot_pos:", shot_pos, "weapon_rot:", weapon_rotation)
        var bullet = bullet_scene.instantiate()
        bullet.position = shot_pos
        bullet.direction = weapon_rotation
        bullet.rotation = other_rotation 

The only way right now to fix it is to signal out to the main node and _process the instantiation there, but I literally have another project sitting in my Godot folder in which I’m able to run this at the After trying to debug it for 2 full days, I’m super frustrated and just want to understand why this is happening.

If you are using the global_position to get the spawn position of the bullet, use the bullet’s global_position and not the position.

Also, set the Projectiles node to CanvasItem.top_level to avoid the bullets following the player movement.

1 Like

Thank you so much, that worked! I hadn’t read about conflicts happening when mixing global positions with (local? relative?) positions. Thanks again!

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