Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

In godot, how do you blend upper and lower body animations using mouse input? I want to…

  1. User

    In godot, how do you blend upper and lower body animations using mouse input? I want to pass on mouse movement into an animation blend node (as it plays upper and lower body animations) to make the character torso point at the same location the camera is pointing.

  2. Assistant

    In Godot, you can blend upper and lower body animations using an AnimationTree with BlendSpace nodes, and then input the mouse movement to drive the blend values. Here's a step-by-step process to achieve this:

    1. Create an AnimationTree node as a child of your animated character.

    2. Set the Animation Player property of the AnimationTree to your character's animation player node.

    3. Set the Animation Tree node's Active property to true.

    4. For the tree's root, create a new Blend2 node, which will be blending the output of two BlendSpaces that control the upper and lower body animations.

    5. Create two BlendSpace1D nodes in the tree, and set up their blend points to have the desired lower body animations (like idle, walk, run) and upper body animations (like aim up, aim middle, aim down).

    6. Connect the BlendSpace1D nodes to the inputs of the Blend2 node.

    7. Connect the Blend2 node to the AnimationTree output node.

    8. Create a script that captures the mouse input and maps it to blend values for both BlendSpace1D nodes.

    Here's a sample script as a starting point:

    extends Spatial
    
    onready var animation_tree = $AnimationTree
    onready var aim_blend_space = animation_tree.get("parameters/UpperBodyAimSpace/blend_position")
    onready var move_blend_space = animation_tree.get("parameters/LowerBodyMoveSpace/blend_position")
    
    export var mouse_sensitivity: float = 0.25
    var camera_angle: float = 0.0
    
    func _input(event):
    	if event is InputEventMouseMotion:
    		apply_mouse_movement(event.relative.y)
    
    func apply_mouse_movement(relative_y):
    	camera_angle -= mouse_sensitivity * relative_y
    	camera_angle = clamp(camera_angle, -90, 90)
    	
    	# Set the blend values for the upper body aim space
    	animation_tree[aim_blend_space] = remap_value(camera_angle, -90, 90, 0, 1)
    	
    	# Example how to set the blend values for lower body movemenBlendSpace1D
    	animation_tree[move_blend_space] = 0.0 # Set it to an appropriate value, e.g., based on player speed
    
    func remap_value(value, old_min, old_max, new_min, new_max):
    	return (value - old_min) / (old_max - old_min) * (new_max - new_min) + new_min
    

    Attach this script to your character node and adjust the names of the nodes accordingly.

    In this example, the camera's vertical angle changes the blend value of the upper body BlendSpace1D node. Modify the script and add additional logic as needed for your specific use case, like blending the lower body animations based on character movement/speed.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.