Godot Version
4.3
Question
Hi! I have recently been wanting to make a game similar to that of dungreed (a 2d action roguelike), where a player can slash a sword twice. Slashing the sword once would make the player slash the sword out and rotate the sword 200ish degrees. Slashing the sword again brings the sword back to the original position. (See below image)
In addition, the sword position is based on where the player’s mouse is. It can move anywhere around the player as the mouse moves. My question is, how would I program this in Gdscript? Below is my code so far,
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -300.0
var currentFacing = 1
var global_mouse_position
var attackPos = 0
@onready var _animation_player = $AnimationPlayer
@onready var player: Sprite2D = $"player sprite"
@onready var weapon_sprite: Sprite2D = $"player sprite/sword offset/weapon sprite"
@onready var sword_offset: Marker2D = $"player sprite/sword offset"
func _physics_process(delta: float) -> void:
global_mouse_position = get_global_mouse_position()
sword_offset.look_at(get_global_mouse_position())
if Input.is_action_just_pressed("attack"):
if attackPos == 0:
weapon_sprite.look_at(get_global_mouse_position())
sword_offset.position.x = 7
weapon_sprite.rotate(0)
attackPos = 1
elif attackPos == 1:
weapon_sprite.look_at(get_global_mouse_position())
sword_offset.position.x = 4
weapon_sprite.rotate(-2.0944)
attackPos = 0
I also have the players sprite 2d have a child marker node that has a child for the sword sprite. The marker node is supposed to act like a “shoulder” for the weapon sprite to move around.
The main issue for me is that the pivot point changes every click, centering on the player’s hand. (you can see this when comparing image A to image B), as well as the fact that my sword looks at the global mouse position every delta frame, which disrupts the slashing by resetting the position of the sword 60 times a second.
Thanks for taking the time to read this, if there’s any questions about my question please ask! I’ll take any help that I can get!