Animation tree for aiming is buggy

Godot Version

4.2.1

Question

`the animation tree for aiming doesnt work at all kinda, right animation for aim doesnt come up, down and up come but too late. I kinda give up and made it 4 directions instead of 8 ways.

up is set to (0,-1)
down is set to (0,1)
right is set to (1,0)
left is set to (-1,0)

ezgif-7d2091e6179cb7


and this is the code for aiming:

@onready var state_machine = animation_tree["parameters/playback"]
var animation_state_keys = ["idle", "run", "gun", "gunshot", "Save"]
var blend_position_paths = [
	"parameters/idle/idle_bs2d/blend_position",
	"parameters/run/run_bs2d/blend_position", 
	"parameters/gun/gun_bs2d/blend_position",
	"parameters/gunshot/gunshot_bs2d/blend_position",
	"parameters/Save/save_bs2d/blend_position"
]
func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		var aim_vector = event.global_position - global_position
		var normalized_aim_direction = aim_vector.normalized()
		blend_position = normalized_aim_direction

func animate() -> void:
		animation_tree.set(blend_position_paths[current_state], blend_position)
	state_machine.travel(animation_state_keys[current_state])

I got so much desperate that I asked 4 different AIs but nothing…

What is blend_position? Does it have a setter that changes the blend position of the gun_bs2d blendspace? Also, what are those 16 messages in the debugger?

1 Like

var blend_position: Vector2 = Vector2.ZERO
The normalized_aim_direction is a Vector2. By assigning this vector to blend_position, im using the direction of the player aiming

also the debugs are for warning for some of my bad coding, its not related to the animation

if you’re declaring the blend_position variable yourself, where do you use it to set the actual blend position of the animation?

1 Like

i think this is your answer:

@onready var state_machine = animation_tree["parameters/playback"]
var animation_state_keys = ["idle", "run", "gun", "gunshot", "Save"]
var blend_position_paths = [
	"parameters/idle/idle_bs2d/blend_position",
	"parameters/run/run_bs2d/blend_position", 
	"parameters/gun/gun_bs2d/blend_position",
	"parameters/gunshot/gunshot_bs2d/blend_position",
	"parameters/Save/save_bs2d/blend_position"
]

func animate() -> void:
		animation_tree.set(blend_position_paths[current_state], blend_position)
	state_machine.travel(animation_state_keys[current_state])

sorry i just copied what a youtuber did…

ok I found what the problem was… jesus its been months that im dealing with this problem and now i debugged it… when I put a print for normalized_aim_direction I saw that if the position of the character changes and the cursor stayed in the same position, the variable changes… so long story short, the code was the problem. this is the correct one:

func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		var mouse_global_pos = get_global_mouse_position()
		var aim_vector = mouse_global_pos - global_position
		var distance = aim_vector.length()
		print(distance)
		
		# deadzone radius
		if distance > 10:  # 10 pixels threshold
			blend_position = aim_vector.normalized()
1 Like