How to use root motion to move a KinematicBody

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Matthew Goulart

Hey guys. I’m learning godot and so far it’s been a delight!

I am wondering how I can go about getting my player character to move.
My “character” scene is set up as follows:

low_poly_guy
= Armature
== Skeleton
=== Mesh
= Animation Player
= Animation Tree
= Root Motion View
= Kinematic Body
== Collision Shape

The animation tree is correctly set up, I know this because the Root Motion View moves according to the position of my character’s hips. This is a combination of mixamo animations exported with blender BTW.

So I save this scene and add it to my level, and then attach this script:

extends Node

func _input(event):
	if event.is_action_pressed("ui_up"):
		$AnimationTree["parameters/speed/blend_amount"] = 0
		var xform = $AnimationTree.get_root_motion_transform()
		$KinematicBody.move_and_slide(Vector3(	0, 0, 1))
	
	if event.is_action_released("ui_up"):
		$AnimationTree["parameters/speed/blend_amount"] = -1

So the animation plays properly, but the character doesn’t move at all.

Any thoughts? Thanks!

:bust_in_silhouette: Reply From: Thakee Nathees

$AnimationTree.get_root_motion_transform() will gives the transformation of the root motion, apply it to your KinematicBody

var rmt = $player/AnimationTree.get_root_motion_transform()
var velocity = ((transform * rmt).origin - transform.origin) / delta
move_and_slide(velocity, Vector3.UP)