Hi folks, got a weird one. So I’m trying to recreate a game using similar code to Alien Attack and applying what I’m learning so far (practicing). Got the player and enemy movement down pat no worries and all the nodes and scenes are in place. However the projectile (I’m using the icon.svg squished down) to be a projectile rather than the rocket file as per the course and now the projectile is boomeranging backwards as you see in the GIF below:
Here’s the code I’ve got for the player:
extends CharacterBody2D
@export var speed = 300
var bullet_scene = preload("res://Scenes/bullet.tscn")
@onready var bullet_container = $BulletContainer
func _process(delta):
if Input.is_action_just_pressed("shoot"):
_shoot()
func _physics_process(delta):
velocity = Vector2(0,0)
if Input.is_action_pressed("move_right"):
velocity.x = speed
if Input.is_action_pressed("move_left"):
velocity.x = -speed
if Input.is_action_pressed("move_up"):
velocity.y = -speed
if Input.is_action_pressed("move_down"):
velocity.y = speed
move_and_slide()
var screen_size = get_viewport_rect().size
global_position = global_position.clamp(Vector2(0,0), screen_size)
func _shoot():
var bullet_instance = bullet_scene.instantiate()
bullet_container.add_child(bullet_instance)
bullet_instance.global_position = global_position
bullet_instance.global_position.x += 50
And the projectile:
extends Area2D
@export var speed = 200
func _physics_process(delta):
global_position.x = speed*delta
func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()
And the nodes I have set for the player and bullet: