3D character controller using root motion
-
Hello, everyone!
I've been loving my Godot journey so far and got a working 3D third person character controller. Now my focus is transitioning it to root motion.
Got some Mixamo animations and added the root bone using a Blender add-on. The RootMotionView node is picking the movement up nicely.
I am only able to move the character on the Z axis. Here's my code (Godot 4.1):
_physics_process(delta: float): var root_motion = animation_tree.get_root_motion_position() var input_direction := Input.get_vector("left", "right", "forward", "back") var direction := transform.basis * Vector3(input_direction.x, 0, input_direction.y) direction = direction.rotated(Vector3.UP, spring_arm.rotation.y).normalized() if is_on_floor(): velocity = direction * root_motion / delta if direction: mesh.rotation.y = lerp_angle(mesh.rotation.y, atan2(-velocity.x, -velocity.z)
animation_tree.get_root_motion_position() returns vectors like (-0.000207, 0, 0.077832) so I'm sure that it's related to the velocity I'm setting.
Thanks for reading my post, any help is appreciated!
-
Figured it out. Here's the code for anyone interested:
_physics_process(delta: float): var root_motion := animation_tree.get_root_motion_position() var input_direction := Input.get_vector("left", "right", "forward", "back") var direction := transform.basis * Vector3(input_direction.x, 0, input_direction.y) var current_rotation := transform.basis.get_rotation_quaternion().normalized() var horizontal_rotation = camera_rig.global_transform.basis.get_euler().y var horizontal_velocity = current_rotation * root_motion / delta direction = Vector3(input_direction.x, 0, input_direction.y) direction = direction.rotated(Vector3.UP, horizontal_rotation).normalized() velocity = Vector3(horizontal_velocity.x, velocity.y, horizontal_velocity.z) if direction: rotation.y = lerp_angle(rotation.y, atan2(direction.x, direction.z), lerp_weight)