CoyoteTimer 的原理:
- 在第一次离开地面时启动CoyoteTimer
- 在CoyoteTimer启动的时间内可以在空中进行跳跃
检测第一次离开地面只需要检测move_and_slide()之前的是否在地面状态是否等于move_and_slide()后的在地面上的状态。
用WasOnFloor来存储前一个is_on_floor(),并在move_and_slide()后进行检测即可,如果变化了,说明是第一次离开地面,启动土狼时间。
在跳跃的条件下加入土狼时间启动这一条件。
extends KinematicBody2D
var gravity = 1000 # 重力值
var velocity = Vector2.ZERO # 玩家最终的速度应用值
var maxHorizontalSpeed = 125 # 水平速度的上限值: 玩家的速度将会被限制在该范围内
var horizontalAcceleration = 2000 # 按下按键后会持续获得的水平加速度
var jumpSpeed = 360 # 玩家按下按键的起始向上速度 / 本模板中玩家可以最多跳上3格的方块
var jumpTerminationMultiplier = 4 # 用于提前停止玩家的跳跃的参数 -> 33行
func _process(delta):
var moveVector = get_movement_vector()
# 进行玩家的实际移动 #
velocity.x += moveVector.x * horizontalAcceleration * delta # 按下按键后给予水平加速度
############## 松开按键进行减速
if (moveVector.x == 0):
velocity.x = lerp(velocity.x, 0, pow(2, -50 * delta)) # 让速度从原来的速度变到 0, 重量为 pow(2, -25 * delta)
############## 限制玩家的速度防止超过最大值
velocity.x = clamp(velocity.x, -maxHorizontalSpeed, maxHorizontalSpeed)
####################################################
############## 按下跳跃键进行跳跃
if (moveVector.y == -1 and (is_on_floor() || !$CoyoteTimer.is_stopped())):
# 如果玩家按下了跳跃键才进行移动,或者玩家出在第一次掉出地面的0.1秒内的土狼时间内也可以跳跃
velocity.y = moveVector.y * jumpSpeed
#### 提前停止跳跃
if (velocity.y < 0 && !Input.is_action_pressed("jump")):
velocity.y += gravity * jumpTerminationMultiplier * delta # 当没有按下按键时增强重力来达到提前落地的作用
else:
velocity.y += gravity * delta # 让玩家受到重力的作用
var WasOnFloor = is_on_floor()
velocity = move_and_slide(velocity, Vector2.UP) # 移动玩家并检测碰撞
# 注意: move_and_slide()方法需要提供速度和向上的法向量
if (WasOnFloor && !is_on_floor()): # 一旦我们第一次离开地面,启动土狼时间
$CoyoteTimer.start()
update_animation()
func get_movement_vector():
# 获取玩家按键输入的脚本
# 检测玩家移动的按键并存入moveVecor向量中
var moveVector = Vector2.ZERO
moveVector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
moveVector.y = -1 if Input.is_action_just_pressed("jump") else 0
# 移动数组用来存储玩家的按键比特值
return moveVector
func update_animation():
var moveVec = get_movement_vector()
if (! is_on_floor()): # 如果不在地面上播放跳跃动画
$AnimatedSprite.play("jump")
elif (moveVec.x != 0): # 如果左右方向进行了移动播放跑步动画
$AnimatedSprite.play("run")
else: # 否则静止
$AnimatedSprite.play("idle")
if (moveVec.x != 0): # 只有提供输入才会更新翻转,否则翻转停留在上一次的状态下。
$AnimatedSprite.flip_h = true if moveVec.x > 0 else false