Godot Version
Godot 4
Question
for a little a bit of context am currently working a 2d top down RPG with 4 directional character and am currently stuck on the character changing animation direction not to be confused with just rotating the character i want when the mouse position changes that animation direction changes towards what direction the mouse is facing and currently stuck any help would great
here my player code:
extends CharacterBody2D
@export var speed: float = 100
@onready var animation = $PlayerAnimation
@onready var weapon = $WeaponFX
@onready var hitbox = $HitBox
@export var projectile_node : PackedScene
@export var current_item : Item:
set(value):
current_item = value
if current_item != null:
if current_item.animation in ["blade"]:
set_damage(current_item.damage)
else:
set_damage(1)
func _ready() → void:
if current_item: set_damage(current_item.damage)
get_node(“HitBox”)
pass
var lastAnimDirection : String = “down”
var time : float = 0
var gun_equiped = false
var mouse_loc_from_player = null
var can_move : bool = true:
set(value):
can_move = value
if value == false:
speed = 0
else:
speed = 100
func handleInput():
var moveDirection = Input.get_vector(“left”, “right”, “up”, “down”)
velocity = moveDirection*speed
if Input.is_action_just_pressed("attack") and time <= 0:
Attack()
func Attack():
can_move = false
time = 0.6
if current_item == null:
animation.play(“fist” + lastAnimDirection)
else:
$Weapon.visible = true
$Weapon.texture = current_item.texture
weapon.play(current_item.animation + lastAnimDirection)
animation.play(current_item.weapon_animation + lastAnimDirection)
func updateAnimation():
if can_move:
if velocity.length() == 0:
animation.play(“idle” + lastAnimDirection)
else:
var direction = “down”
if velocity.x < 0: direction = “left”
elif velocity.x > 0: direction = “right”
elif velocity.y < 0: direction = “up”
animation.play("walk" + direction)
lastAnimDirection = direction
if current_item.gun_equiped:
if mouse_loc_from_player.x >= -70 and mouse_loc_from_player.x <= 70 and mouse_loc_from_player.y < 0:
animation.play("shootup")
if mouse_loc_from_player.y >= -25 and mouse_loc_from_player.y <= 25 and mouse_loc_from_player.x < 0:
animation.play("shootright")
if mouse_loc_from_player.x >= -25 and mouse_loc_from_player.x <= 25 and mouse_loc_from_player.y < 0:
animation.play("shootdown")
if mouse_loc_from_player.y >= -25 and mouse_loc_from_player.y <= 25 and mouse_loc_from_player.x < 0:
animation.play("shootleft")
func _physics_process(delta):
mouse_loc_from_player = get_global_mouse_position() - self.position
print(mouse_loc_from_player)
if time > 0:
time -= delta
handleInput()
move_and_slide()
updateAnimation()
func set_damage(amount):
if hitbox != null:
hitbox.damage = amount
func _on_player_animation_animation_finished(anim_name: StringName) → void:
can_move = true
func shoot_projectile():
var mouse_pos = get_global_mouse_position()
var projectile = projectile_node.instantiate()
$Marker2D.look_at(mouse_pos)
projectile.rotation = $Marker2D.rotation
projectile.global_position = $Marker2D.global_position
projectile.damage = current_item.damage
get_tree().current_scene.add_child(projectile)
here is my projectile code:
extends Area2D
var direction : Vector2 = Vector2.RIGHT
var damage : float = 1
var speed = 350
func _physics_process(delta: float) → void:
position += (Vector2.RIGHT*speed).rotated(rotation) * delta
func _on_visible_on_screen_notifier_2d_screen_exited() → void:
queue_free()
func _on_body_entered(body):
if body.has_method(“take_damage”):
body.take_damage(damage)