How can I create Melee Attack based on Mouse direction

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cookieoil

I want to make melee attack like this game Bright Lancer by Slo Nod which will trigger everytime you click the mouse button. Beside that, my code is following to this https://www.youtube.com/watch?v=gEx_Fmf-MhU&t=107s, a tutorial about playing 8 direction animations with the mouse, of course. I try to combine it with melee attack but seem like they aren’t doing well, debugs show up everytime and thus, whenever u attack too much, the function will be null and stop playing animation, here is the code for more closer view:

var AttackPoints = 3;

var s = 0
var i = 0
var a = 0
var b = 0
var c = 0

func _process(delta:float):
   if isplaying == false:
	var mouse = get_local_mouse_position()
	s = stepify(mouse.angle(), PI/4) / (PI/4)
	s = wrapi(int(s), 0, 8)
	
	if Input.is_action_just_pressed("skill"):
		i = s
	
	if Input.is_action_just_pressed("attack"):
		a = s
		b = s
		c = s

func move_state(delta:float):
   if Input.is_action_just_pressed("attack") and AttackPoints == 3:
	$AttackResetTimer.start();
	state = ATTACK;
	AttackPoints = AttackPoints - 1;
	
  elif Input.is_action_just_pressed("attack") and AttackPoints == 2:
	$AttackResetTimer.start();
	state = ATTACK2;
	AttackPoints = AttackPoints - 1;
	
  elif Input.is_action_just_pressed("attack") and AttackPoints == 1:
	$Attack3ResetTimer.start();
	state = ATTACK3;
	AttackPoints = AttackPoints - 1;
	
func attack_state() -> void:
  isplaying = false;
  animationTree.active = false;
  animationPlayer.playback_active = true;
  current_animation = "Attack";
  animationPlayer.play(current_animation + str(a));
  animationPlayer.playback_speed = 1.75;
  isplaying = true;

 if isplaying == true:
	a = null

func attack2_state() -> void:
 $Sprite.visible = false
 $Sprite2.visible = true
 isplaying = false
 animationTree.active = false
 animationPlayer.playback_active = true
 current_animation = "AttackTwo"
 animationPlayer.play(current_animation + str(b))
 animationPlayer.playback_speed = 1.75
 isplaying = true

 if isplaying == true:
	b = null

func attack3_state() -> void:
 $Sprite.visible = false
 $Sprite2.visible = true
 isplaying = false
 animationTree.active = false
 animationPlayer.playback_active = true
 current_animation = "AttackThree"
 animationPlayer.play(current_animation + str(c))
 animationPlayer.playback_speed = 1.75
 isplaying = true

 if isplaying == true:
	c = null

func attack_animation_finished()->void:
 if state == ATTACK || state == ATTACK2 || state == ATTACK3:
	$Sprite.visible = true
	$Sprite2.visible = false
	isplaying = false
	current_animation = "Idle"
	animationPlayer.play(current_animation + str(s))
	isstop = false
	state = MOVE

	if isstop == false:
		s = null

I want to fix this as quick as possible, so ask me if you guys need more information (big thanks for the help)

:bust_in_silhouette: Reply From: MisterMano

Giving us the debug message would’ve helped, too. Which function returns null? stepify, wrap, move_state? Which function(s) stop working if you attack too much?

One thing I noticed from your code is that AttackPoints never resets back to 3. I don’t know if you left that piece out of this code or if you really forgot about it. It looks like it should go into the attack_animation_finished() function.

Another thing that doesn’t make much sense is the a b c. From the looks of it, you could’ve used s on all 3 attack states without any problem or difference, unless the attacks queue up, each with their own direction.

isplaying is set to false, then true shortly afterwards, without any apparent check or use in between. Why? Also, why check if it’s true when you literally set it to true on the line above it? (Protip, when dealing with boolean variables, if varname: works better than if varname == true)

First of all, from the bottom of my heart, I’m so sorry that my question does giving you the annoying that you don’t deserve, I haven’t really gave any effort for this one, I have made another question already, which I hope can satisfy all of your suggestions.

Of course, if you don’t might, I will put the link here:
How can I create Melee attack based on mouse direction? - Archive - Godot Forum

(you are under no obligation to answer my question again, but of course, if you do, I will really appreciate)

Once again, I’m really sorry that my question had giving you an unpleasant experience that you don’t want and further more, my bad English too.

cookieoil | 2021-10-30 11:42