【发布时间】:2021-08-19 00:38:59
【问题描述】:
这是我的第一个项目之一,我正在按照 YT 上的教程编写此代码。据我所知,本教程不包括有关动画的内容,因此我尝试自己完成它并且空闲和运行动画工作。跳跃也有效,但它在稍微延迟后开始并且没有完成它的循环,因为角色着陆太快(对于最后一个问题,我会尝试调整动画的速度)
扩展演员
func _physics_process(delta):
var direction = get_direction()
velocity = calculate_move_velocity(velocity, direction, maxSpeed)
velocity = move_and_slide(velocity, FLOOR_NORMAL) #Funzione che permette il movimento del personaggio
func get_direction() -> Vector2:
return Vector2(
Input.get_action_strength("right") - Input.get_action_strength("left"),
-1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 0.0
)
func calculate_move_velocity( #Movimento e Animazioni
linear_velocity: Vector2,
direction: Vector2,
maxSpeed: Vector2
) -> Vector2:
var new_velocity = linear_velocity #la new_velocity sarà il movimento lineare del personaggio
new_velocity.x = maxSpeed.x * direction.x
new_velocity.x = lerp(new_velocity.x, 0, 0.1)
if is_on_floor() and direction.x == 1.0: #muoversi verso destra
$AnimationPlayer.play("run")
$Sprite.scale.x = 1
elif is_on_floor() and direction.x == -1.0: #muoversi verso sinistra
$AnimationPlayer.play("run")
$Sprite.scale.x = -1
if is_on_floor() and direction.x == 0.0: #stare fermi
$AnimationPlayer.play("idle")
new_velocity.y += gravity * get_physics_process_delta_time()
if direction.y == -1.0: #saltare
new_velocity.y = maxSpeed.y * direction.y
if !is_on_floor() == false and Input.is_action_just_pressed("jump"):
$AnimationPlayer.play("jump")
return new_velocity
【问题讨论】: