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?