2D How to have the right animation play according to where I shoot the projectile (mouse direction)

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

Hello and thanks for taking the time and trying to help me.

I have been able to create a projectile (firespell) and have it shoot where I aim my mouse.
I can figure out as well to play an animation as I fire the projectile, so that the player will start cast animation.

What I can´t do is:

  1. Have the player animate in the right direction, so for example: I cast a spell behind the player I would like the player face that side and cast the animation.

Basically I want a for the player to understand where I cast the projectile and based on that use the right animation ( I have 4directional characters, so up, down, left right.). I cant have them face diagonally because I dont have animations for those so based on what direction is closest to the mouse I want my player to use that animation.

I will upload my projectile script and player script as much as relevant to the question.

PROJECTILE SCRIPT

extends Node2D

var motion = Vector2.ZERO

func _process(delta):
	translate(motion * delta)
	#print(position.y)
	#print(position.x)
	

#Here new conditions can lead for destruction of the spell
func destroy():
	self.queue_free()
	
#Deletes the spell after it has been active for ( timer, currently 5s (changeable in Inspector))
func _on_Timer_timeout():
	destroy()

#Deletes the spell if it exits the players screen
func _on_VisibilityNotifier2D_screen_exited():
	destroy()

func _on_Area2D_body_entered(body):
	destroy()

PLAYER SCRIPT

export(PackedScene) var FIRESPELL: PackedScene = preload("res://Scenes/FireSpell.tscn")

func _process(delta):
	if Input.is_action_just_pressed("TESTCast"): fire_spell()


func fire_spell():
	if FIRESPELL:
		var firespell = FIRESPELL.instance()
		get_tree().current_scene.add_child(firespell)
		firespell.global_position = $SpellPosition.global_position
		var firespell_rotation = self.global_position.direction_to(get_global_mouse_position()).angle()
		firespell.rotation = firespell_rotation

Thank you so much for helping me, have been looking at many tutorials and I really am a beginner, so implementing simple code in mine can also be troublesome. All examples are appreciated to be given as accurately as possible with examples to integrate.
Thanks again!

:bust_in_silhouette: Reply From: zhyrin

As you use get_global_mouse_position(), there is also get_local_mouse_position().
If x is lower than 0, the mouse is to the left of the player, if y is lower than 0, the mouse is above the player.
Based on these you can detrmine which animation you want to play.

Hei, thanks alot, I tried and tried based on what you said but finally got it :smiley: thank you again!

Cryothenir | 2023-03-08 21:09