Help with smoothing and blendspace

Hellooo !

I’m currently trying to smoothly animate my character.
For this I’ve created an AnimPlayer as a NodeBlendTree and in there added a BlendSpace2D for my walking in all directions animations.

Everything works except the smooth animations, I’ve read on the following post that I should use lerp() to smooth the values I send to the blendspace2d but I’m having trouble with the lerp function and how it works.

In _physics_process, i have this :

var moving := Input.get_vector("left", "right", "forward", "backward")

if current_speed <= BASE_SPEED:
	animTree.set("parameters/MovType/transition_request", "walking")
	animTree.set("parameters/LocomotionWalking/blend_position", moving)

I can see that my values from input_dir are between -1 and 1 on both the x and y as the previous post talked about.

Could someone point me in the right direction to smooth those values ?
I tried that but ended with very strange results :

moving.x = lerp(0.0, moving.x, delta)
moving.y = lerp(0.0, moving.y, delta)

Any help would be greatly appreciated!
Thanks :slight_smile:

i’m not sure i completely understand how you code works but you could try something like:
moving = lerp(moving, input_dir, delta)
this will make the moving variable smoothly follow input_dir
edit: moving needs to be declared outside of _physics_process, so it doesn’t “reset” every frame. you can multiply delta to make the transition faster.

Thanks for the reply, sorry for the misunderstanding, the input_dir is actually the moving variable, I forgot to rename it in the example I posted here.
I will try what you said and report back.

Okay, so I’ve tried to do what you proposed but can’t seem to get it right.

This is my code at the moment :


func _physics_process(delta: float) -> void:
	var input_dir := Input.get_vector("left", "right", "forward", "backward")

	# Only update state if playing can move, otherwise set to idle.
	if is_jumping_enabled or is_running_enabled or is_walking_enabled:	
		
		# Walking
		if current_speed <= BASE_SPEED:
			animTree.set("parameters/MovType/transition_request", "walking")
			animTree.set("parameters/LocomotionWalking/blend_position", input_dir)
		else: # Running
			animTree.set("parameters/MovType/transition_request", "running")
			animTree.set("parameters/LocomotionRunning/blend_position", input_dir)

The input_dir retrieves a vector based on my WASD keys and then passes it to the animation blend 2d in the animation tree, and then plays the right animations.

How would I move input_dir outside of physics process ? I’ve tried creating another variable based on the last state from input_dir and then lerping between the two but it doesn’t work…

Thanks for your help.

is this how you tried it? input_dir doesn’t have to be outside of _physics_process, the variable that is being lerped does, and that should be used to set the animations.

var anim_blend := Vector2.ZERO

func _physics_process(delta: float) -> void:
	var input_dir := Input.get_vector("left", "right", "forward", "backward")

    anim_blend = lerp(anim_blend, input_dir, delta * 5)

	# Only update state if playing can move, otherwise set to idle.
	if is_jumping_enabled or is_running_enabled or is_walking_enabled:	
		
		# Walking
		if current_speed <= BASE_SPEED:
			animTree.set("parameters/MovType/transition_request", "walking")
			animTree.set("parameters/LocomotionWalking/blend_position", anim_blend)
		else: # Running
			animTree.set("parameters/MovType/transition_request", "running")
			animTree.set("parameters/LocomotionRunning/blend_position", anim_blend)

This works! Thanks a lot, I’ll try to understand in more details for the near future.

Have a great day, thank you :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.