Godot Version 4.7 stable
Hello there, my name is Markiplier and welcome to your biweekly show of “How Do I Code This?” in today’s episode, I have a weapon resource for making different weapons (guns) and now that I´m implementing them (instantiating them through a weapon controller component) I want to flip the sprites of said resources (guns) according to the position of the mouse cursor relative to the character.
when the mouse.global_position.x > the player, it looks like this
and yet, when is not, looks like this:
I’m instantiating the weapon through this weapon controller:
class_name WeaponController extends Node
@export var current_weapon: WeaponGeneral
@export var weapon_model_parent: Node2D
var current_weapon_model: Node2D
func _ready():
if current_weapon:
spawn_weapon_model()
func spawn_weapon_model():
if current_weapon_model:
current_weapon_model.queue_free()
if current_weapon.weapon_model:
current_weapon_model = current_weapon.weapon_model.instantiate()
weapon_model_parent.add_child(current_weapon_model)
current_weapon_model.position = current_weapon.weapon_position
attached to a marker in the arm sprite:
class_name QuackyMainScript extends CharacterBody2D
@onready var weapon_controller = $WeaponController
@export var Body: Sprite2D
@export var Arm: Sprite2D
var mouse_position = get_global_mouse_position()
func _physics_process(delta:float) -> void:
Arm.look_at(mouse_position)
Arm.rotation += deg_to_rad(270)
#flips the sprite so tat it looks normal
if Body.global_position.x < mouse_position.x:
Body.flip_h = true
else:
Body.flip_h = false
here is my tree scene:
Quacky
┠╴Body
┃ ┖╴Arm
┃ ┖╴Gun marker
┃ ┖╴BasicPistol
┃ ┖╴Pistol Sprite
┠╴CollisionHead
┠╴CollisionBody
┠╴HitboxBody
┃ ┖╴DetectionForHitBody
┠╴HitboxHead
┃ ┖╴DetectionForHitHead
┠╴HitboxTail
┃ ┖╴DetectionForHitTail
┖╴WeaponController
So, I want to know how to fix this sprite, called using a component, from a gun resource

