• Godot Help2D
  • Shoot a projectile in direction player is facing

I'm trying to make my first game and recently finished playing cuphead. And now I'm stuck on shooting the projectile the right way. I've tried a lot of attemps, but it either didnt work at all or crashed or changed the way it flew based on if the player is going left or right. So I want to shoot a projectile in direction where the player is facing but it only flyes right currently. pls help.

bullet.gd

extends CharacterBody2D

@export var speed = 600
var velo = Vector2(1,0)
@export var maker = 0

func _physics_process(delta):
	var collision_info = move_and_collide(velo.normalized() * delta * speed)

CharacterBody2D.gd

extends CharacterBody2D

signal laser_shot(laser_scene, location)

@onready var marker_2d = $Marker2D
@onready var sprite_2d = $Sprite2D
@onready var character_body_2d = $"."
@export var rate_of_fire := 0.25




var laser_scene = preload("res://bullet.tscn")

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

var shoot_cd := false

func _process(delta):
	if Input.is_action_pressed("shoot"):
		if !shoot_cd:
			shoot_cd = true
			shoot()
			await get_tree().create_timer(rate_of_fire).timeout
			shoot_cd = false
			
func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
		
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	
	if direction >0:
		
		sprite_2d.flip_v = false
		marker_2d.global_position.x = character_body_2d.global_position.x +80
	elif direction <0:
		
		sprite_2d.flip_v = true
		marker_2d.global_position.x = character_body_2d.global_position.x -80
		
	
	move_and_slide()
	

	
func shoot():
	laser_shot.emit(laser_scene, marker_2d.global_position)

game.gd

extends Node2D

var player = null
@onready var player_spawn_pos = $PlayerSpawnPos
@onready var character_body_2d = $"."
@onready var lasery = $Lasery

func _ready():
	character_body_2d.global_position = player_spawn_pos.global_position
	


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass



func _on_character_body_2d_laser_shot(laser_scene, location):
	var laser = laser_scene.instantiate()
	laser.global_position = location
	lasery.add_child(laser)
    func _physics_process(delta):
    	var collision_info = move_and_collide(velo.normalized() * delta * speed)

    lafycek this is godot 3 code. are you using godot 3?

    lafycek var laser = laser_scene.instantiate()

    and this is godot 4 code. huh

    lafycek crashed

    please post screenshots of the error messages?

    lafycek extends CharacterBody2D

    @export var speed = 600
    var velo = Vector2(1,0)
    @export var maker = 0

    func _physics_process(delta):
    var collision_info = move_and_collide(velo.normalized() * delta * speed)

    your projectile is a characterbody, and you never tell it which direction to go.
    where did you get this code? did you write it yourself or is it from some tutorial?

    the structure of this code is odd and... bad.
    but to give the laser a direction you would have to first add a parameter to the signal:

    laser_shot(laser_scene, location, direction)

    then give it a direction the player is facing in shot():

    func shoot():
    	laser_shot.emit(laser_scene, marker_2d.global_position, direction)

    and then change velo.x when instantiating the scene in game.gd:

    func _on_character_body_2d_laser_shot(laser_scene, location, direction):
    	var laser = laser_scene.instantiate()
    	laser.global_position = location
    	lasery.add_child(laser)
    	velo.x = direction