Godot Version
Godot Engine v4.5.dev4.official.209a446e3
Question
I implemented basic jump functionality for the player using the following code, but the jump and the animation are not well synchronized. How can I fix this issue? I suspect the problem is caused by a 0.36-second crouch anticipation phase at the beginning of the jump animation. This crouch makes the jump feel more dynamic, so I’d prefer not to modify the animation itself. Given this, is there any effective way to resolve the mismatch?
...
@export var skin_body_height: float = 1.85 # 身体高度
@export var skin_jump_height: float = 1.60 # 跳跃高度
@export var skin_jump_time_to_peak: float = 0.4 # 跳跃达到峰值时间
@export var skin_jump_time_to_descent: float = 0.3 # 跳跃下降时间
@onready var skin_jump_velocity: float = ((skin_body_height * skin_jump_height) / skin_jump_time_to_peak) * -1.0 # 跳跃速度
@onready var skin_jump_gravity: float = ((-skin_body_height * skin_jump_height) / (skin_jump_time_to_peak * skin_jump_time_to_peak)) * -1.0 # 跳跃重力
@onready var skin_jump_fall_gravity: float = ((-skin_body_height * skin_jump_height) / (skin_jump_time_to_descent * skin_jump_time_to_descent)) * -1.0 # 下落重力
func jump_logic(_delta: float) -> void:
# 如果在地面时
if is_on_floor():
# 如果触发了跳跃输入事件
if Input.is_action_just_pressed("jump"):
set_move_state("ground_stand_start_f_jump_lfoot")
velocity.y = -player_body_skin.skin_jump_velocity
else:
set_move_state("ground_stand_start_f_jump_lfoot")
var jump_gravity = player_body_skin.skin_jump_gravity if velocity.y > 0.0 else player_body_skin.skin_jump_fall_gravity
velocity.y -= jump_gravity * _delta