200字
lua脚本:Missile_track
2025-11-22
2025-11-26

默认加载火箭弹上面。

local Script = {}
function Script:OnStart()
    self:AddTriggerEvent(TriggerEvent.MissileCreate, self.ff)
-- 组件启动时调用
end
function Script:ff(event)
    local index = Player:GetShotcutIndex(event.eventobjid)
    local id=event.itemid
    if index<=4 and id==12285 then
        local playerid=event.eventobjid
        threadpool:wait(0.1)
        local missileX, missileY, missileZ = Actor:GetPosition(event.toobjid)
        local searchRadius = 100 -- 搜索半径
        local areaStart = {x = missileX - searchRadius, y = missileY - searchRadius, z = missileZ - searchRadius} -- 搜索区域起点
        local areaEnd = {x = missileX + searchRadius, y = missileY + searchRadius, z = missileZ + searchRadius} -- 搜索区域终点
        local targetType = ObjType.Mob -- 目标类型:生物
        local objIds = Area:GetAllObjsInAreaRange(areaStart, areaEnd, ObjType.Mob)
        if #objIds == 0 then return end
        -- 找到最近的目标
        local nearestDistance = math.huge
        local targetX, targetY, targetZ = nil, nil, nil
        for _, objId in ipairs(objIds) do
            local objX, objY, objZ = Actor:GetPosition(objId)
             -- 计算欧几里得距离
            local distance = math.sqrt((missileX - objX)^2 + (missileY - objY)^2 + (missileZ - objZ)^2)
            if distance < nearestDistance then
                nearestDistance = distance
                targetX, targetY, targetZ = objX, objY, objZ
            end
        end
        if targetX and targetY and targetZ then
        -- 检测投掷物路径前方是否有方块并销毁
        local step = 0.6
        local directionX = (targetX - missileX) / nearestDistance
        local directionY = (targetY - missileY) / nearestDistance
        local directionZ = (targetZ - missileZ) / nearestDistance
        for i = 1, math.floor(nearestDistance / step) do
            local checkX = missileX + directionX * step * i
            local checkY = missileY + directionY * step * i
            local checkZ = missileZ + directionZ * step * i
            local blockID = Block:GetBlockID(math.floor(checkX), math.floor(checkY), math.floor(checkZ))
            if blockID ~= 0 then -- 检测到非空气方块
                Block:DestroyBlock(math.floor(checkX), math.floor(checkY), math.floor(checkZ), true) -- 销毁方块
            end
        end
        -- 清除当前投掷物
        World:DespawnActor(event.toobjid)
            -- 随机偏移量,使投掷物路径不完全笔直
            local randomOffsetX = 0
            local randomOffsetY = math.random(0, 0.3) -- 保证一定的向上偏移
            local randomOffsetZ = 0
        -- 生成新的投掷物
        World:SpawnProjectile(
                playerid,
                12285, -- 投掷物 ID
                missileX, missileY, missileZ, 
                targetX + randomOffsetX, targetY + randomOffsetY, targetZ + randomOffsetZ, -- 终点(带随机偏移)
                math.random(60,70) 
            )
        end
    end
end
return Script

lua脚本:Missile_track
作者
站长
发表于
2025-11-22

评论