Root Motion Code not working (Edited)

Godot Version

Godot 4.2.2

Question

Hi, I am trying to implement a root motion system in my game but it’s currently not working

Expected Output:

Current output:

As you can see, the animation is being played. However, the character itself isn’t moving

Below is how I implemented this system. I would be more than delighted if any kind soul may take some time to go through it and point out any problems with how I have implemented it

Animation Tree:

Transition to Walking:

Transition from Walking:

The Walking Blendspace:

Code:

var	stamina:	ProgressBar = self.get_node("../../UI/Stamina_Bar")
var	anim:		Node = $"../StateMachine"
var	playBack:	AnimationNodeStateMachinePlayback = anim.get("parameters/playback")
var	input_dir:	Vector2 = Input.get_vector("Move Left", "Move Right", "Move Forward", "Move Back")
var	dir:		Vector3 = (head.transform.basis * Vector3(-input_dir.y, 0, input_dir.x)).normalized()
var	rot:		Quaternion = self.get_quaternion()
	
anim.set("parameters/conditions/moving", dir != Vector3.ZERO)
anim.set("parameters/Walking/blend_position", -input_dir)
self.set_velocity(rot * anim.get_root_motion_position())
move_and_slide()

Thanks in advance

PS: The post has been edited as the previous post was lacking in information and quality

1 Like

For future questions, please just copy/paste your code and surround it with 3 tick marks (key to the left of 1 on most keyboards) to format it:

```
func _ready():
print(“foo”)
```

Becomes

    func _ready():
        print("foo")

Screenshots of code are not easy to work with.

To your question:

What does the print(self.velocity) show? Also, why divide by delta when setting a velocity? delta is already used internally in move_and_slide() with velocity to determine how the position should change. (Also, you’re in _process(delta), so you can just use delta instead of self.get_process_delta_time()).

I don’t know what your StateMachine node looks like, so I have no idea what get_root_motion_position is supposed to do. I think you should either provide more code, or add more print statements for each value to see which one is not doing what you think it should do.

  1. What does the print(self.velocity) show?
  • It was a debug message I forgotten to remove
  1. Why divide by delta when setting a velocity?
  • I was under the impression that as the movement is calculated overtime, I would need to divide it by delta. Does that mean I only need to divide a value by delta if that value isn’t being used by move_and_slide()?
  1. You’re in _process(delta), so you can just use delta instead of self.get_process_delta_time())
  • I saw a post online that stated every process has their own delta value and for animations there’s a special delta value that they use and the way to access that value is through that function

The move_and_slide function takes no arguments and uses the velocity of the CharacterBody, taking delta into account already.

1 Like

I had a very similar issue and for me the cause was that Godot was not recognizing the root properly. The fix was to go into import settings for the model → add a bone map under retarget → add a skeleton profile → go to the root at the bottom of the feet of the skeleton profile and set the root bone as that root. That was the root cause, no pun intended. As soon as the correclty flagged root bone is translated in an animation track, it was displayed in place now and the motion was only visible with a rootmotionview node in the scene, when you are looking at the animations executed by the animationtree. But you also have to make sure the root bone is actually translated in the animation track as well.