Godot Version
Question
<i am working on a top down shooter it will be my second game and i cant get shooting to work properly when i shoot it spawns the bullets to my side and not forward this is my code
extends CharacterBody2D
var bullet_speed = 2000
var speed = 200
var bullet_scene = preload(“res://bullet.tscn”)
func _physics_process(delta):
# Create a 2D direction vector based on input
var move_direction = Vector2.ZERO
if Input.is_action_pressed("forward"):
move_direction += Vector2.UP
if Input.is_action_pressed("backwards"):
move_direction += Vector2.DOWN
if Input.is_action_pressed("left"):
move_direction += Vector2.LEFT
if Input.is_action_pressed("right"):
move_direction += Vector2.RIGHT
# ... (rest of movement input code)
# Normalize the direction for consistent speed
move_direction = move_direction.normalized()
# Set the velocity directly
velocity = move_direction * speed
# Move the character using move_and_slide (without arguments)
move_and_slide()
# Get the mouse position and offset it
# Look at the offset mouse position
look_at(get_global_mouse_position())
if Input.is_action_pressed("fire"):
fire()
func fire():
Check for fire rate (your existing logic)
# Create bullet instance (assuming bullet_scene is preloaded or loaded correctly)
var bullet_instance = ResourceLoader.load("res://bullet.tscn").instantiate()
# Set bullet position (assuming it starts at character's position)
bullet_instance.global_position = global_position
var forward_direction = bullet_instance.rotation.normalized();
bullet_instance.apply_central_impulse(forward_direction * bullet_speed);
# Set bullet rotation (assuming it should face the same direction as the character)
bullet_instance.rotation = rotation ++ 90
# Get the forward direction based on rotation (assuming rotation is a Vector2)
# Apply impulse in the forward direction
# Apply impulse in the firing direction (modify as needed)
# Add the bullet to the scene
get_tree().get_root().add_child(bullet_instance)