So, a couple years ago, there was a post where someone attempted to replicate a top down attack system from someones develop. The way it worked was:
- The sword rotates around a 'pivot' of sorts and faced the mouses direction.
- The player would flip from facing left to right based on whether the mouse is on the left side or the right side of the mouse.
- There is an attack animation using a tween node.
The way I have the sword set up currently is:
- The root node is a StaticBody2D
- The child (currently) is an AnimatedSprite2D (this is because I want to add different swords to the game when I get there, so it would be much simpler to just swap animations, change the damage and add other effects based on what sword is being used) Here is my current code, and I'll explain the problems with it below:
extends CharacterBody2D
u/export var speed = 100
u/export var friction = 0.09
u/export var acceleration = 0.2
func get_input():
var input = Vector2()
if Input.is_action_pressed('right'):
input.x += 1
if Input.is_action_pressed('left'):
input.x -= 1
if Input.is_action_pressed('down'):
input.y += 1
if Input.is_action_pressed('up'):
input.y -= 1
return input
func _physics_process(delta):
animate()
particle_handle()
get_global_mouse_position()
var direction = get_input()
if direction.length() > 0:
velocity = velocity.lerp(direction.normalized() * speed, acceleration)
else:
velocity = velocity.lerp(Vector2.ZERO, friction)
move_and_slide()
var mouse_position = get_viewport().get_mouse_position()
$sword.rotation_degrees = rad_to_deg(mouse_position.angle_to_point(global_position))
if mouse_position.x < 90:
$player_anims.flip_h = true
$sword.position.x = -13
else:
$player_anims.flip_h = false
$sword.position.x = 13
func animate():
if Input.is_action_pressed("left"):
$player_anims.play("run")
elif Input.is_action_pressed("right"):
$player_anims.play("run")
elif Input.is_action_pressed("down"):
$player_anims.play("run")
elif Input.is_action_pressed("up"):
$player_anims.play("run")
else:
$player_anims.play("idle")
func particle_handle():
if Input.is_action_pressed("down"):
$walk_dust.emitting = true
elif Input.is_action_pressed("right"):
$walk_dust.emitting = true
elif Input.is_action_pressed("left"):
$walk_dust.emitting = true
elif Input.is_action_pressed("up"):
$walk_dust.emitting = true
else:
$walk_dust.emitting = false
As we have with my current code, my player only flips when the mouse is at the edge of the screen. Also, when my sword is facing the right, it faces upside down. Just to help you guys:
var mouse_position = get_viewport().get_mouse_position()
$sword.rotation_degrees = rad_to_deg(mouse_position.angle_to_point(global_position))
if mouse_position.x < 90:
$player_anims.flip_h = true
$sword.position.x = -13
else:
$player_anims.flip_h = false
$sword.position.x = 13
This part here is responsible for flipping everything. There's nothing in the StaticBody2D script. If you want to help with tween nodes too, that would be great, as I've only spent about 3 weeks consecutively using Godot 4.1, so I'm a beginner. If you need any other bits of information to help, just ask. Thanks!
P.S: https://www.youtube.com/watch?v=6BrZryMz-ac
Youtube vid for reference.