2D Platformer Shooter

Godot Version

4.1.3.stable

Question

I’m trying to figure out how to make my bullet go in the right direction when facing right

this is my player code

extends CharacterBody2D

@export_group("Player Variables")
@export var speed = 1200
@export var jump_speed = -1800
@export var gravity = 4000
@export_range(0.0, 1.0) var friction = 0.1
@export_range(0.0 , 1.0) var acceleration = 0.25

@export var Bullet: PackedScene


func _physics_process(delta):
	velocity.y += gravity * delta
	
	#getting input direction
	var dir = Input.get_axis("left", "right")
	
	#moving and sliding
	if dir != 0:
		velocity.x = lerp(velocity.x, dir * speed, acceleration)
	else:
		velocity.x = lerp(velocity.x, 0.0, friction)
	
	#flipping sprite to the movement direction
	if dir > 0:
		%Sprite2D.flip_h = true
	elif dir < 0:
		%Sprite2D.flip_h = false
	
	move_and_slide()
	
	#jumping
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_speed
	
	#shooting
	if Input.is_action_just_pressed("shoot"):
		shoot()


func shoot():
	var b = Bullet.instantiate()
	owner.add_child(b)
	b.transform = %BulletSpawner.global_transform

this is my bullet code

extends Area2D

var speed = 750

func _physics_process(delta):
	position += transform.x * speed * delta


func _on_body_entered(body):
	queue_free()

I’m very new to programming and especially Godot. I figured out how to get my character to look left and right when moving with the if loop- but I’m not sure how to make the marker2d and direction of the bullet move too as the marker2d doesn’t have a flip variable and neither does the characterbody2d and those were my two ideas on how to make this happen. Am I just completely missing it? Thanks in advance.

I see you’re using the top-down shooting projectile tutorial.
From your project, I can see, you’re working on a side scroller. To adapt your code to a side scroller without changing much in your current setup, the way I would do it is to keep track of the players’ direction and pass that direction to the bullet to determine which side it will travel. This will only work for left and right bullet movement and not diagonal.

In the Player script, add:


enum _direction{LEFT = -1, RIGHT = 1}
var curretnt_direction: _direction = _direction.RIGHT #Set this to whatever direction the player faces on scene load

func _physics_process(delta):
        ...
	
	#flipping sprite to the movement direction and track direction
	if dir > 0:
		%Sprite2D.flip_h = true
        current_direction = _direction.RIGHT
	elif dir < 0:
		%Sprite2D.flip_h = false
        current_direction = _direction.LEFT

         ...

func shoot():
	var b = Bullet.instantiate()
	owner.add_child(b)
    b.direction = current_direction

In the bullet script:

extends Area2D

var speed = 750
var direction = 1 ##Set default direction of bullet

func _physics_process(delta):
	position += direction * speed * delta


func _on_body_entered(body):
	queue_free()

What we’re doing is essentially just to move the bullet in the direction the player was facing when it was shot.

AH I was trying to figure out how to do it like this too! thank you! this makes sense

I am however running into an error on the bullet script “Invalid operands ‘Vector2’ and ‘float’ in operator ‘+’” on the line of code in physics process.

Change position to position.x since we’re only changing the x position.

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