2d shooting mechanic not working

I am having trouble with the shooting mechanics of my project. I followed this tutorial “Make a 2D TOP-DOWN SHOOTER in just 10 MINUTES (Godot Game Engine)” by Eli Cuaycong, it is an outdated tutorial, but I couldn’t find any better ones. Whenever I try to fire the bullet the bullet itself appears behind the player and kind of just randomly move away or stay in one spot unless touched or spawn in facing random directions.


Here’s the player code

extends CharacterBody2D


var speed = 500
var bullet_speed = 2000
var bullet = preload("res://Bullet.tscn")




func _physics_process(delta):
	var direction = Vector2()
	
	if Input.is_action_pressed("up"):
		direction.y -= 1
	if Input.is_action_pressed("down"):
		direction.y += 1
	if Input.is_action_pressed("left"):
		direction.x -= 1
	if Input.is_action_pressed("right"):
		direction.x += 1
	
	if direction.length() > 0:
		direction = direction.normalized() * speed
	
	position += direction * delta
	
	move_and_slide()
	
	look_at(get_global_mouse_position())

	if Input.is_action_just_pressed("left click"):
		fire_bullet()


func fire_bullet():
	var bullet_instance = bullet.instantiate() 
	bullet_instance.position = global_position
	bullet_instance.rotation_degrees = rotation_degrees
	bullet_instance.linear_velocity = Vector2(bullet_speed,0).rotated(rotation)
	get_tree().get_root().call_deferred("add_child", bullet_instance)

What did I do wrong and how do I get the player to fire bullets correctly?

Taking a stab here, I think we need to see the code for bullet.physics_process(delta), and possibly also Player.look_at(vec2).

As far as the bullet appearing behind the player, check the Z-Index value in Inspector > Ordering

is your player scene offset? Make sure the player’s visuals are at 0,0 and it’s CharacterBody2D has no rotation or position changes. Furthermore is there any parent of the player that is offset or rotated?

I managed to get it working, thank you for your help

It was offset and after fixing it, it now works. Thank you for bringing that to my attention

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