Godot Version
4.2.2
Question
So just a disclaimer I’m a beginner and I’m messing around trying to make a game and here is the problem I faced. I made a scene with a tree and when the player is in the vicinity of the tree and the player presses the action button, the player should play the axe-swinging animation(used hoe swing animation because im lazy). It seems to be working properly however the animation is all messed up. I will try to include as many screenshots as possible someone please help.
player.gd:
extends CharacterBody2D
@export var move_speed : float = 100
@export var starting_direction : Vector2 = Vector2(0,0.5)
@export var inv : Inv
@export var move_input : Vector2
var harvestable = false
# parameters/idle/blend_position
# parameters/walk/blend_position
@onready var animation_tree = $AnimationTree
func _ready():
animation_tree.active = true
update_animation_parameters(starting_direction)
func _physics_process(_delta):
# Get input direction
var input_direction = Vector2(
Input.get_action_strength("right") - Input.get_action_strength("left"),
Input.get_action_strength("down") - Input.get_action_strength("up")
)
update_animation_parameters(input_direction)
# Update velocity
velocity = input_direction * move_speed
if Input.is_action_just_pressed("shift"):
move_speed *= 1.25
if Input.is_action_just_released("shift"):
move_speed = 100
move_and_slide()
pick_new_state()
func update_animation_parameters(move_input):
if(move_input != Vector2.ZERO):
animation_tree.set("parameters/idle/blend_position", move_input)
animation_tree.set("parameters/walk/blend_position", move_input)
animation_tree.set("parameters/hoe_swing/blend_position", move_input)
func pick_new_state():
if (velocity != Vector2.ZERO):
animation_tree["parameters/conditions/is_moving"] = true
animation_tree["parameters/conditions/idle"] = false
else:
animation_tree["parameters/conditions/idle"] = true
animation_tree["parameters/conditions/is_moving"] = false
if (harvestable):
if (Input.is_action_just_pressed("Action")):
animation_tree["parameters/conditions/swing"] = true
await get_tree().create_timer(1.5).timeout
animation_tree["parameters/conditions/swing"] = false
func player():
pass
func collect(item):
inv.insert(item)
func Harvest():
update_animation_parameters(move_input)
harvestable = true
func notharvestable():
harvestable = false
tree.gd:
extends Node2D
var player_in_area = false
var player = null
var numOfHits = 0
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
if player_in_area:
if Input.is_action_just_pressed("Action"):
player.Harvest()
func _on_harvestable_area_body_entered(body):
if body.has_method("player"):
player_in_area = true
player = body
func _on_harvestable_area_body_exited(body):
if body.has_method("player"):
player_in_area = false
player.notharvestable()
