Animation glitching when trying to change root motion

Godot Version

Godot 4.7.1 stable

Question

Hello, I’'m only 2 weeks into learning how to program especially GD script. I don’t know why the enemy animation glitches when I tried to change the root motion between animations. here’s the scene code. any help is appreciated :smile:

extends characterbody3D

var player: Node3D = null
var state_machine

const speed = 4
const attack_range = 1.5

@onready var nav_agent = $NavigationAgent3D
@onready var anim_tree = $AnimationTree



func _ready():
    
    #get player
    await get_tree().physics_frame
    player = get_tree().get_first_node_in_group("player")
    
    state_machine = anim_tree.get("parameters/playback")

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
    
    var in_range = _target_in_range()
    
    #search player
    if not player:
        return
    
    velocity = Vector3.ZERO
    
    match state_machine.get_current_node():
        "running":
            if in_range:
                velocity = Vector3.ZERO
            else:
                #Navigation
                nav_agent.set_target_position(player.global_transform.origin)
                var next_nav_point = nav_agent.get_next_path_position()
                velocity = (next_nav_point - global_transform.origin).normalized() * speed
            
            look_at(Vector3(global_position.x + velocity.x, global_position.y, global_position.z + velocity.z), Vector3.UP)
        "attack":
            anim_tree.root_motion_track = ^"Skeleton3D:Mixamorig_RightLeg"
    
    #conditions
    anim_tree.set("parameters/conditions/attack", in_range)
    anim_tree.set("parameters/conditions/running", !in_range)
    
    anim_tree.get("parameters/playback")
    
    move_and_slide()

func _target_in_range():
    return global_position.distance_to(player.global_position) < attack_range

You don’t need the root motion

Okay I’ve found out that for this problem, I don’t actually need to root motion anything. I just needed to remove the hips animation in the first place which keeps the character forward. removing the root motion also removes the weird glitch in the first place.