我需要帮助来将方向向量设置为每45度一次。我游戏用的框架是Godot 4.6.2,我想让角色可以在8个方向(每45度一次)移动。下面是我的代码:
func _physics_process(delta):
agent.target_desired_distance = 28 # 限制距离
if Root._player:
agent.target_position = Root._player.position # 设定目标位置
if agent.is_navigation_finished(): # 检测是否完成
return
var next_path_position = agent.get_next_path_position() # 获取下一个路径位置
var direction = (next_path_position - global_position).normalized() # 计算方向
# 如果角度大于 45 度,设定为 45 度的分隔点
var degrees = direction.angle() * RAD2DEG
if degrees > 45 and sign(degrees - 45) != sign(degrees - 135):
direction = QUAT_FROM_TO_EULER(Vector3(1, 0, 0), direction, 45 * deg2rad)
velocity = direction * speed # 应用方向和速度
move_and_slide()
这里使用了 QUAT_FROM_TO_EULER 函数将方向转换为 45 度的分隔点。这个函数将为你创建一个在 8 个方向之间转换的向量克amping功能。
评论 (0)