feat: adding better animations
This commit is contained in:
+226
-114
@@ -9,14 +9,26 @@ var root_pivot: Node3D
|
||||
var torso: MeshInstance3D
|
||||
var head_pivot: Node3D
|
||||
var head: MeshInstance3D
|
||||
var arm_l_pivot: Node3D
|
||||
var arm_l: MeshInstance3D
|
||||
var arm_r_pivot: Node3D
|
||||
var arm_r: MeshInstance3D
|
||||
var leg_l_pivot: Node3D
|
||||
var leg_l: MeshInstance3D
|
||||
var leg_r_pivot: Node3D
|
||||
var leg_r: MeshInstance3D
|
||||
|
||||
var upper_arm_l_pivot: Node3D
|
||||
var upper_arm_l: MeshInstance3D
|
||||
var lower_arm_l_pivot: Node3D
|
||||
var lower_arm_l: MeshInstance3D
|
||||
|
||||
var upper_arm_r_pivot: Node3D
|
||||
var upper_arm_r: MeshInstance3D
|
||||
var lower_arm_r_pivot: Node3D
|
||||
var lower_arm_r: MeshInstance3D
|
||||
|
||||
var thigh_l_pivot: Node3D
|
||||
var thigh_l: MeshInstance3D
|
||||
var calf_l_pivot: Node3D
|
||||
var calf_l: MeshInstance3D
|
||||
|
||||
var thigh_r_pivot: Node3D
|
||||
var thigh_r: MeshInstance3D
|
||||
var calf_r_pivot: Node3D
|
||||
var calf_r: MeshInstance3D
|
||||
|
||||
# Animation State Variables
|
||||
var current_state: String = "idle"
|
||||
@@ -57,51 +69,97 @@ func _ready() -> void:
|
||||
head.cast_shadow = shadow_setting
|
||||
head_pivot.add_child(head)
|
||||
|
||||
# Arms
|
||||
var a_mesh = BoxMesh.new()
|
||||
a_mesh.size = Vector3(0.12, 0.65, 0.12)
|
||||
a_mesh.material = mat
|
||||
# Arms (Upper: 0.35 length, Lower: 0.35 length)
|
||||
var ua_mesh = BoxMesh.new()
|
||||
ua_mesh.size = Vector3(0.12, 0.35, 0.12)
|
||||
ua_mesh.material = mat
|
||||
var la_mesh = BoxMesh.new()
|
||||
la_mesh.size = Vector3(0.1, 0.35, 0.1)
|
||||
la_mesh.material = mat
|
||||
|
||||
arm_l_pivot = Node3D.new()
|
||||
arm_l_pivot.position = Vector3(-0.28, 1.45, 0) # Shoulder
|
||||
root_pivot.add_child(arm_l_pivot)
|
||||
arm_l = MeshInstance3D.new()
|
||||
arm_l.mesh = a_mesh
|
||||
arm_l.position = Vector3(0, -0.3, 0) # Offset down from shoulder
|
||||
arm_l.cast_shadow = shadow_setting
|
||||
arm_l_pivot.add_child(arm_l)
|
||||
# Left Arm
|
||||
upper_arm_l_pivot = Node3D.new()
|
||||
upper_arm_l_pivot.position = Vector3(-0.28, 1.45, 0) # Shoulder
|
||||
root_pivot.add_child(upper_arm_l_pivot)
|
||||
upper_arm_l = MeshInstance3D.new()
|
||||
upper_arm_l.mesh = ua_mesh
|
||||
upper_arm_l.position = Vector3(0, -0.175, 0)
|
||||
upper_arm_l.cast_shadow = shadow_setting
|
||||
upper_arm_l_pivot.add_child(upper_arm_l)
|
||||
|
||||
arm_r_pivot = Node3D.new()
|
||||
arm_r_pivot.position = Vector3(0.28, 1.45, 0) # Shoulder
|
||||
root_pivot.add_child(arm_r_pivot)
|
||||
arm_r = MeshInstance3D.new()
|
||||
arm_r.mesh = a_mesh
|
||||
arm_r.position = Vector3(0, -0.3, 0) # Offset down from shoulder
|
||||
arm_r.cast_shadow = shadow_setting
|
||||
arm_r_pivot.add_child(arm_r)
|
||||
lower_arm_l_pivot = Node3D.new()
|
||||
lower_arm_l_pivot.position = Vector3(0, -0.35, 0) # Elbow
|
||||
upper_arm_l_pivot.add_child(lower_arm_l_pivot)
|
||||
lower_arm_l = MeshInstance3D.new()
|
||||
lower_arm_l.mesh = la_mesh
|
||||
lower_arm_l.position = Vector3(0, -0.175, 0)
|
||||
lower_arm_l.cast_shadow = shadow_setting
|
||||
lower_arm_l_pivot.add_child(lower_arm_l)
|
||||
|
||||
# Legs
|
||||
var l_mesh = BoxMesh.new()
|
||||
l_mesh.size = Vector3(0.15, 0.8, 0.15)
|
||||
l_mesh.material = mat
|
||||
# Right Arm
|
||||
upper_arm_r_pivot = Node3D.new()
|
||||
upper_arm_r_pivot.position = Vector3(0.28, 1.45, 0) # Shoulder
|
||||
root_pivot.add_child(upper_arm_r_pivot)
|
||||
upper_arm_r = MeshInstance3D.new()
|
||||
upper_arm_r.mesh = ua_mesh
|
||||
upper_arm_r.position = Vector3(0, -0.175, 0)
|
||||
upper_arm_r.cast_shadow = shadow_setting
|
||||
upper_arm_r_pivot.add_child(upper_arm_r)
|
||||
|
||||
leg_l_pivot = Node3D.new()
|
||||
leg_l_pivot.position = Vector3(-0.12, 0.8, 0) # Hip
|
||||
root_pivot.add_child(leg_l_pivot)
|
||||
leg_l = MeshInstance3D.new()
|
||||
leg_l.mesh = l_mesh
|
||||
leg_l.position = Vector3(0, -0.4, 0) # Offset down from hip
|
||||
leg_l.cast_shadow = shadow_setting
|
||||
leg_l_pivot.add_child(leg_l)
|
||||
lower_arm_r_pivot = Node3D.new()
|
||||
lower_arm_r_pivot.position = Vector3(0, -0.35, 0) # Elbow
|
||||
upper_arm_r_pivot.add_child(lower_arm_r_pivot)
|
||||
lower_arm_r = MeshInstance3D.new()
|
||||
lower_arm_r.mesh = la_mesh
|
||||
lower_arm_r.position = Vector3(0, -0.175, 0)
|
||||
lower_arm_r.cast_shadow = shadow_setting
|
||||
lower_arm_r_pivot.add_child(lower_arm_r)
|
||||
|
||||
leg_r_pivot = Node3D.new()
|
||||
leg_r_pivot.position = Vector3(0.12, 0.8, 0) # Hip
|
||||
root_pivot.add_child(leg_r_pivot)
|
||||
leg_r = MeshInstance3D.new()
|
||||
leg_r.mesh = l_mesh
|
||||
leg_r.position = Vector3(0, -0.4, 0) # Offset down from hip
|
||||
leg_r.cast_shadow = shadow_setting
|
||||
leg_r_pivot.add_child(leg_r)
|
||||
# Legs (Thigh: 0.45 length, Calf: 0.45 length)
|
||||
var t_leg_mesh = BoxMesh.new()
|
||||
t_leg_mesh.size = Vector3(0.15, 0.45, 0.15)
|
||||
t_leg_mesh.material = mat
|
||||
var c_leg_mesh = BoxMesh.new()
|
||||
c_leg_mesh.size = Vector3(0.13, 0.45, 0.13)
|
||||
c_leg_mesh.material = mat
|
||||
|
||||
# Left Leg
|
||||
thigh_l_pivot = Node3D.new()
|
||||
thigh_l_pivot.position = Vector3(-0.12, 0.85, 0) # Hip (raised slightly for 0.45+0.45=0.9 total)
|
||||
root_pivot.add_child(thigh_l_pivot)
|
||||
thigh_l = MeshInstance3D.new()
|
||||
thigh_l.mesh = t_leg_mesh
|
||||
thigh_l.position = Vector3(0, -0.225, 0)
|
||||
thigh_l.cast_shadow = shadow_setting
|
||||
thigh_l_pivot.add_child(thigh_l)
|
||||
|
||||
calf_l_pivot = Node3D.new()
|
||||
calf_l_pivot.position = Vector3(0, -0.45, 0) # Knee
|
||||
thigh_l_pivot.add_child(calf_l_pivot)
|
||||
calf_l = MeshInstance3D.new()
|
||||
calf_l.mesh = c_leg_mesh
|
||||
calf_l.position = Vector3(0, -0.225, 0)
|
||||
calf_l.cast_shadow = shadow_setting
|
||||
calf_l_pivot.add_child(calf_l)
|
||||
|
||||
# Right Leg
|
||||
thigh_r_pivot = Node3D.new()
|
||||
thigh_r_pivot.position = Vector3(0.12, 0.85, 0) # Hip
|
||||
root_pivot.add_child(thigh_r_pivot)
|
||||
thigh_r = MeshInstance3D.new()
|
||||
thigh_r.mesh = t_leg_mesh
|
||||
thigh_r.position = Vector3(0, -0.225, 0)
|
||||
thigh_r.cast_shadow = shadow_setting
|
||||
thigh_r_pivot.add_child(thigh_r)
|
||||
|
||||
calf_r_pivot = Node3D.new()
|
||||
calf_r_pivot.position = Vector3(0, -0.45, 0) # Knee
|
||||
thigh_r_pivot.add_child(calf_r_pivot)
|
||||
calf_r = MeshInstance3D.new()
|
||||
calf_r.mesh = c_leg_mesh
|
||||
calf_r.position = Vector3(0, -0.225, 0)
|
||||
calf_r.cast_shadow = shadow_setting
|
||||
calf_r_pivot.add_child(calf_r)
|
||||
|
||||
func update_state(state: String, speed: float, is_crouching: bool = false) -> void:
|
||||
current_state = state
|
||||
@@ -110,7 +168,6 @@ func update_state(state: String, speed: float, is_crouching: bool = false) -> vo
|
||||
current_state = "crouch"
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
# Advance time. For walk, speed it up based on movement speed.
|
||||
var anim_speed = 1.0
|
||||
if current_state == "ground" and movement_speed > 1.0:
|
||||
anim_speed = movement_speed * 0.4
|
||||
@@ -120,107 +177,162 @@ func _process(delta: float) -> void:
|
||||
# Target values
|
||||
var t_root_pos := Vector3.ZERO
|
||||
var t_root_rot := Vector3.ZERO
|
||||
var t_arm_l_rot := Vector3.ZERO
|
||||
var t_arm_r_rot := Vector3.ZERO
|
||||
var t_leg_l_rot := Vector3.ZERO
|
||||
var t_leg_r_rot := Vector3.ZERO
|
||||
var t_upper_arm_l_rot := Vector3.ZERO
|
||||
var t_upper_arm_r_rot := Vector3.ZERO
|
||||
var t_lower_arm_l_rot := Vector3.ZERO
|
||||
var t_lower_arm_r_rot := Vector3.ZERO
|
||||
var t_thigh_l_rot := Vector3.ZERO
|
||||
var t_thigh_r_rot := Vector3.ZERO
|
||||
var t_calf_l_rot := Vector3.ZERO
|
||||
var t_calf_r_rot := Vector3.ZERO
|
||||
|
||||
match current_state:
|
||||
"ground", "idle":
|
||||
if movement_speed > 1.0:
|
||||
# Walk / Run
|
||||
var swing = sin(_anim_time * 15.0)
|
||||
t_arm_l_rot.x = swing * 0.8
|
||||
t_arm_r_rot.x = -swing * 0.8
|
||||
t_leg_l_rot.x = -swing * 0.8
|
||||
t_leg_r_rot.x = swing * 0.8
|
||||
# Run cycle
|
||||
var swing = sin(_anim_time * 5.0)
|
||||
# Hips
|
||||
t_thigh_l_rot.x = -swing * 1.6
|
||||
t_thigh_r_rot.x = swing * 1.6
|
||||
# Knees bend backwards (positive X rotation) when leg swings back
|
||||
# We add a bit of bend even when moving forward so it's not stiff
|
||||
t_calf_l_rot.x = max(0.1, swing * 1.6 + 0.3)
|
||||
t_calf_r_rot.x = max(0.1, -swing * 1.6 + 0.3)
|
||||
|
||||
# Arms
|
||||
t_upper_arm_l_rot.x = swing * 1.0
|
||||
t_upper_arm_r_rot.x = -swing * 1.0
|
||||
# Elbows bend forwards (negative X rotation)
|
||||
t_lower_arm_l_rot.x = -1.2 + max(0.0, -swing * 0.5)
|
||||
t_lower_arm_r_rot.x = -1.2 + max(0.0, swing * 0.5)
|
||||
|
||||
# Bounce slightly
|
||||
t_root_pos.y = abs(cos(_anim_time * 15.0)) * 0.1
|
||||
t_root_pos.y = abs(cos(_anim_time * 5.0)) * 0.1
|
||||
else:
|
||||
# Idle
|
||||
var breath = sin(_anim_time * 2.0)
|
||||
t_root_pos.y = breath * 0.02
|
||||
t_arm_l_rot.z = 0.05 + breath * 0.01
|
||||
t_arm_r_rot.z = -0.05 - breath * 0.01
|
||||
t_upper_arm_l_rot.z = 0.05 + breath * 0.01
|
||||
t_upper_arm_r_rot.z = -0.05 - breath * 0.01
|
||||
t_lower_arm_l_rot.x = -0.1
|
||||
t_lower_arm_r_rot.x = -0.1
|
||||
t_calf_l_rot.x = 0.05
|
||||
t_calf_r_rot.x = 0.05
|
||||
|
||||
"crouch":
|
||||
t_root_pos.y = -0.5
|
||||
# Bend legs to simulate squat
|
||||
t_leg_l_rot.x = -1.0
|
||||
t_leg_r_rot.x = -1.0
|
||||
# Arms tucked
|
||||
t_arm_l_rot.x = 0.5
|
||||
t_arm_r_rot.x = 0.5
|
||||
# Thighs forward, calves back
|
||||
t_thigh_l_rot.x = -1.0
|
||||
t_thigh_r_rot.x = -1.0
|
||||
t_calf_l_rot.x = 1.0
|
||||
t_calf_r_rot.x = 1.0
|
||||
# Arms bent defensively
|
||||
t_upper_arm_l_rot.x = -0.5
|
||||
t_upper_arm_r_rot.x = -0.5
|
||||
t_lower_arm_l_rot.x = -1.2
|
||||
t_lower_arm_r_rot.x = -1.2
|
||||
|
||||
"slide":
|
||||
t_root_pos.y = -0.6
|
||||
t_root_rot.x = deg_to_rad(-60) # Lean back
|
||||
# Legs extended
|
||||
t_leg_l_rot.x = deg_to_rad(-45)
|
||||
t_leg_r_rot.x = deg_to_rad(60)
|
||||
# Arms swept back for balance
|
||||
t_arm_l_rot.x = deg_to_rad(60)
|
||||
t_arm_r_rot.x = deg_to_rad(60)
|
||||
t_root_rot.x = deg_to_rad(-45) # Lean back
|
||||
# Legs forward
|
||||
t_thigh_l_rot.x = deg_to_rad(-60)
|
||||
t_thigh_r_rot.x = deg_to_rad(-30)
|
||||
t_calf_l_rot.x = 0.1
|
||||
t_calf_r_rot.x = 0.1
|
||||
# Arms back
|
||||
t_upper_arm_l_rot.x = deg_to_rad(60)
|
||||
t_upper_arm_r_rot.x = deg_to_rad(60)
|
||||
t_lower_arm_l_rot.x = -0.2
|
||||
t_lower_arm_r_rot.x = -0.2
|
||||
|
||||
"air":
|
||||
# Falling / Jumping
|
||||
t_leg_l_rot.x = -0.2
|
||||
t_leg_r_rot.x = 0.2
|
||||
t_arm_l_rot.x = -0.5
|
||||
t_arm_r_rot.x = -0.5
|
||||
t_arm_l_rot.z = 0.3
|
||||
t_arm_r_rot.z = -0.3
|
||||
t_thigh_l_rot.x = -0.4
|
||||
t_thigh_r_rot.x = 0.2
|
||||
t_calf_l_rot.x = 0.5
|
||||
t_calf_r_rot.x = 0.1
|
||||
|
||||
t_upper_arm_l_rot.x = -0.8
|
||||
t_upper_arm_r_rot.x = -0.8
|
||||
t_upper_arm_l_rot.z = 0.5
|
||||
t_upper_arm_r_rot.z = -0.5
|
||||
t_lower_arm_l_rot.x = -0.5
|
||||
t_lower_arm_r_rot.x = -0.5
|
||||
|
||||
"wall_run":
|
||||
# Tilt away from wall slightly
|
||||
t_root_rot.z = deg_to_rad(-15)
|
||||
# Fast cycle legs
|
||||
var cycle = sin(_anim_time * 25.0)
|
||||
t_leg_l_rot.x = cycle * 0.6
|
||||
t_leg_r_rot.x = -cycle * 0.6
|
||||
t_arm_l_rot.x = -cycle * 0.6
|
||||
t_arm_r_rot.x = cycle * 0.6
|
||||
t_thigh_l_rot.x = cycle * 0.8
|
||||
t_thigh_r_rot.x = -cycle * 0.8
|
||||
t_calf_l_rot.x = max(0.1, -cycle * 1.0 + 0.4)
|
||||
t_calf_r_rot.x = max(0.1, cycle * 1.0 + 0.4)
|
||||
|
||||
t_upper_arm_l_rot.x = -cycle * 0.8
|
||||
t_upper_arm_r_rot.x = cycle * 0.8
|
||||
t_lower_arm_l_rot.x = -1.0
|
||||
t_lower_arm_r_rot.x = -1.0
|
||||
|
||||
"wall_cling":
|
||||
# Hanging onto the wall
|
||||
t_leg_l_rot.x = deg_to_rad(-45)
|
||||
t_leg_r_rot.x = deg_to_rad(30)
|
||||
t_arm_l_rot.x = deg_to_rad(-160)
|
||||
t_arm_r_rot.x = deg_to_rad(-160)
|
||||
# Spiderman hanging
|
||||
t_thigh_l_rot.x = deg_to_rad(-80)
|
||||
t_thigh_r_rot.x = deg_to_rad(-40)
|
||||
t_calf_l_rot.x = deg_to_rad(80)
|
||||
t_calf_r_rot.x = deg_to_rad(40)
|
||||
|
||||
t_upper_arm_l_rot.x = deg_to_rad(-140)
|
||||
t_upper_arm_r_rot.x = deg_to_rad(-140)
|
||||
t_lower_arm_l_rot.x = deg_to_rad(-40)
|
||||
t_lower_arm_r_rot.x = deg_to_rad(-40)
|
||||
|
||||
"grapple":
|
||||
# One arm forward toward target, legs dangling
|
||||
t_arm_l_rot.x = deg_to_rad(-120)
|
||||
t_arm_r_rot.x = deg_to_rad(10)
|
||||
t_leg_l_rot.x = deg_to_rad(15)
|
||||
t_leg_r_rot.x = deg_to_rad(-10)
|
||||
t_upper_arm_l_rot.x = deg_to_rad(-160)
|
||||
t_lower_arm_l_rot.x = 0.0
|
||||
t_upper_arm_r_rot.x = deg_to_rad(20)
|
||||
t_lower_arm_r_rot.x = deg_to_rad(-90)
|
||||
|
||||
t_thigh_l_rot.x = deg_to_rad(-30)
|
||||
t_calf_l_rot.x = deg_to_rad(30)
|
||||
t_thigh_r_rot.x = deg_to_rad(20)
|
||||
t_calf_r_rot.x = deg_to_rad(10)
|
||||
|
||||
"dash":
|
||||
# Superman pose
|
||||
t_root_rot.x = deg_to_rad(-70)
|
||||
t_arm_l_rot.x = deg_to_rad(-160)
|
||||
t_arm_r_rot.x = deg_to_rad(-160)
|
||||
t_leg_l_rot.x = deg_to_rad(20)
|
||||
t_leg_r_rot.x = deg_to_rad(20)
|
||||
t_upper_arm_l_rot.x = deg_to_rad(-170)
|
||||
t_lower_arm_l_rot.x = 0.0
|
||||
t_upper_arm_r_rot.x = deg_to_rad(-170)
|
||||
t_lower_arm_r_rot.x = 0.0
|
||||
|
||||
t_thigh_l_rot.x = deg_to_rad(20)
|
||||
t_calf_l_rot.x = 0.0
|
||||
t_thigh_r_rot.x = deg_to_rad(20)
|
||||
t_calf_r_rot.x = 0.0
|
||||
|
||||
"death":
|
||||
# Fall backwards like a plank
|
||||
t_root_rot.x = deg_to_rad(-90)
|
||||
t_root_pos.y = -0.5 # Shift down slightly so it rests on floor
|
||||
t_arm_l_rot.x = deg_to_rad(170)
|
||||
t_arm_r_rot.x = deg_to_rad(170)
|
||||
t_leg_l_rot.x = deg_to_rad(-10)
|
||||
t_leg_r_rot.x = deg_to_rad(10)
|
||||
t_root_pos.y = -0.5
|
||||
t_upper_arm_l_rot.x = deg_to_rad(170)
|
||||
t_lower_arm_l_rot.x = 0.0
|
||||
t_upper_arm_r_rot.x = deg_to_rad(170)
|
||||
t_lower_arm_r_rot.x = 0.0
|
||||
t_thigh_l_rot.x = deg_to_rad(-10)
|
||||
t_calf_l_rot.x = 0.0
|
||||
t_thigh_r_rot.x = deg_to_rad(10)
|
||||
t_calf_r_rot.x = 0.0
|
||||
|
||||
# Smoothly interpolate towards target values
|
||||
var lerp_speed = 15.0 * delta
|
||||
var lerp_speed = 40.0 * delta
|
||||
root_pivot.position = root_pivot.position.lerp(t_root_pos, lerp_speed)
|
||||
root_pivot.rotation = _lerp_vec3(root_pivot.rotation, t_root_rot, lerp_speed)
|
||||
|
||||
arm_l_pivot.rotation = _lerp_vec3(arm_l_pivot.rotation, t_arm_l_rot, lerp_speed)
|
||||
arm_r_pivot.rotation = _lerp_vec3(arm_r_pivot.rotation, t_arm_r_rot, lerp_speed)
|
||||
leg_l_pivot.rotation = _lerp_vec3(leg_l_pivot.rotation, t_leg_l_rot, lerp_speed)
|
||||
leg_r_pivot.rotation = _lerp_vec3(leg_r_pivot.rotation, t_leg_r_rot, lerp_speed)
|
||||
upper_arm_l_pivot.rotation = _lerp_vec3(upper_arm_l_pivot.rotation, t_upper_arm_l_rot, lerp_speed)
|
||||
upper_arm_r_pivot.rotation = _lerp_vec3(upper_arm_r_pivot.rotation, t_upper_arm_r_rot, lerp_speed)
|
||||
lower_arm_l_pivot.rotation = _lerp_vec3(lower_arm_l_pivot.rotation, t_lower_arm_l_rot, lerp_speed)
|
||||
lower_arm_r_pivot.rotation = _lerp_vec3(lower_arm_r_pivot.rotation, t_lower_arm_r_rot, lerp_speed)
|
||||
|
||||
thigh_l_pivot.rotation = _lerp_vec3(thigh_l_pivot.rotation, t_thigh_l_rot, lerp_speed)
|
||||
thigh_r_pivot.rotation = _lerp_vec3(thigh_r_pivot.rotation, t_thigh_r_rot, lerp_speed)
|
||||
calf_l_pivot.rotation = _lerp_vec3(calf_l_pivot.rotation, t_calf_l_rot, lerp_speed)
|
||||
calf_r_pivot.rotation = _lerp_vec3(calf_r_pivot.rotation, t_calf_r_rot, lerp_speed)
|
||||
|
||||
func _lerp_vec3(a: Vector3, b: Vector3, t: float) -> Vector3:
|
||||
return Vector3(
|
||||
|
||||
Reference in New Issue
Block a user