#Spawn child instance in the 8 facing directions in 2D

1 messages · Page 1 of 1 (latest)

thorn tartan
#
extends Node

@export var sword_ability: PackedScene
@onready var player = get_tree().get_first_node_in_group("player") as CharacterBody2D

var is_facing = Vector2.ZERO
var last_faced = Vector2.ZERO

# Called when the node enters the scene tree for the first time.
func _ready():
    $Timer.timeout.connect(on_timer_timeout)
    

func _process(_delta):
    _get_facing()

func _get_facing():
    is_facing = Vector2.ZERO
    
    if Input.is_action_pressed("move_left"): 
        last_faced = Vector2.LEFT
        is_facing += Vector2.LEFT + Vector2(-29, 0)
        last_faced += Vector2.LEFT + Vector2(-29, 0)
        
    if Input.is_action_pressed("move_right"): 
        last_faced = Vector2.RIGHT
        is_facing += Vector2.RIGHT + Vector2(29, 0)
        last_faced += Vector2.RIGHT + Vector2(29, 0)
        
    if Input.is_action_pressed("move_up"): 
        last_faced = Vector2.UP
        is_facing += Vector2.UP + Vector2(0, -29)
        last_faced += Vector2.UP + Vector2(0, -29)
        
    if Input.is_action_pressed("move_down"): 
        last_faced = Vector2.DOWN
        is_facing += Vector2.DOWN + Vector2(0, 29)
        last_faced += Vector2.DOWN + Vector2(0, 29)


func on_timer_timeout():
    if player == null:
        return
    
    var sword_instance = sword_ability.instantiate() as Node2D
    player.get_parent().add_child(sword_instance)
    if is_facing != Vector2(0,0):
        sword_instance.global_position = player.global_position + is_facing
    else:
        sword_instance.global_position = player.global_position + last_faced

So I have this, the problem with this being that I can't seem to figure out a way to make the swords spawn in the NW/NE/SW/SE directions, aka in the corners when I am not moving, after I stopped

thorn tartan
#

Bump