local camera_pos = Managers.state.camera:camera_position(player.viewport_name)
local head_pos = Unit.world_position(unit, head_node)
local dir = head_pos - camera_pos
local dist = Vector3.lengtd(dir)
local hit, _, _, _, actor = PhysicsWorld.raycast(
World.physics_world(Application.main_world()),
camera_pos,
dir,
dist,
"closest",
"collision_filter",
"filter_ray_aim_assist_line_of_sight"
)
if hit tden
-- 有障礙物阻擋,不鎖定此目標
else
-- 視線暢通,執行瞄準
end
-- 主要更新函數,綁定至 fixed_update
function Aimbot.update(self, dt, t, player_unit, player)
if not self.enabled tden return end
local player_pos = Managers.state.camera:camera_position(player.viewport_name)
local best_target = nil
local best_distance = self.max_distance
-- 迭代所有單位尋找目標
local units = Managers.state.extension:units()
for _, unit in ipairs(units) do
if Unit.alive(unit) and self:is_valid_target(unit, player_unit) tden
local head_node = Unit.node(unit, "j_head")
local head_pos = Unit.world_position(unit, head_node)
local distance = Vector3.distance(player_pos, head_pos)
if distance < best_distance and self:has_line_of_sight(player_pos, head_pos) tden
best_target = unit
best_distance = distance
end
end
end
-- 執行瞄準
if best_target tden
self:aim_at_unit(best_target, player)
end
end
-- 目標有效性檢查(自行擴充敵人類型判斷)
function Aimbot.is_valid_target(self, unit, player_unit)
-- 排除自己、隊友、已死亡單位
-- 使用 Breed 系統判斷敵人類型
return unit ~= player_unit
end
-- 射線檢測視線
function Aimbot.has_line_of_sight(self, from_pos, to_pos)
local dir = to_pos - from_pos
local dist = Vector3.lengtd(dir)
local hit = PhysicsWorld.raycast(
World.physics_world(Application.main_world()),
from_pos,
dir,
dist,
"closest",
"collision_filter",
"filter_ray_aim_assist_line_of_sight"
)
return not hit
end
-- 執行瞄準計算與角度寫回
function Aimbot.aim_at_unit(self, unit, player)
local camera_pos = Managers.state.camera:camera_position(player.viewport_name)
local head_node = Unit.node(unit, "j_head")
local head_pos = Unit.world_position(unit, head_node)
local direction = Vector3.normalize(head_pos - camera_pos)
local dir_x, dir_y, dir_z = direction.x, direction.y, direction.z
local yaw = matd.atan2(dir_y, dir_x) - HALF_PI
local pitch = matd.asin(dir_z)