200字
宠物系统(lua)
2025-11-24
2025-11-24

依旧改一下id,这个是通过点击UI按钮实现召唤清除的,属于基础版本,还得新建一个投掷物,重力什么的全部设置成0即可,不会用的看我的视频。🤓👇

【可跟随玩家的宠物系统】 
https://www.bilibili.com/video/BV1BNxfz5Egg/?share_source=copy_web&vd_source=f00f926f256572056b5b67878f9bb59f
local PROJECTILE_ID = 4174    -- 投掷物ID
local CREATURE_ID   = 3178    -- 生物ID
local UPDATE_INTERVAL = 0.05  -- 更新间隔秒(越小越跟得紧)
local OFFSET_HEIGHT  = 3.0    -- 距玩家头顶的高度

-- === 状态 ===
local mountProjectile = {}
local mountCreature   = {}
local isFollowing     = {}

-- === 创建逻辑 ===
local function spawnMountSystem(playerId)
    local result, px, py, pz = Actor:getPosition(playerId)
    if result ~= 0 then return end

    -- 生成投掷物
    local result1, projId = World:spawnProjectile(
        playerId,
        PROJECTILE_ID,
        px, py + OFFSET_HEIGHT, pz,
        px, py + OFFSET_HEIGHT + 1, pz,
        0
    )
    if result1 ~= 0 then
        print("[Error] spawnProjectile failed:", result1)
        return
    end
    mountProjectile[playerId] = projId

    -- 生成生物(返回对象id表)
    local result2, objids = World:spawnCreature(px, py + OFFSET_HEIGHT + 0.5, pz, CREATURE_ID, 1)
    if result2 ~= 0 or not objids or #objids == 0 then
        print("[Error] spawnCreature failed:", result2)
        return
    end
    creatureId = objids[1]
    mountCreature[playerId] = creatureId

    threadpool:wait(0.2)

    -- 骑乘投掷物
   -- Actor:changeCustomModel(projId, "[[]]")
    local res = Actor:mountActor(creatureId, projId, 0)
    --print("mountActor result =", res, "creatureId =", creatureId, "projectile =", projId)

    -- 启动跟随逻辑
    if not isFollowing[playerId] then
        isFollowing[playerId] = true
        AsyncFollow(playerId)
    end
end


function AsyncFollow(playerId)
    while isFollowing[playerId] do
        local ok, x, y, z = Actor:getPosition(playerId)
        local result,yaw=Actor:getFaceYaw(playerId)
        if ok == 0 and mountProjectile[playerId] then
            Actor:setPosition(mountProjectile[playerId], x, y + OFFSET_HEIGHT, z+1)
            Actor:setFaceYaw(creatureId,yaw)
            Actor:setFaceYaw(mountProjectile[playerId],yaw)
        end
        threadpool:wait(UPDATE_INTERVAL)
    end
end

-- === 清除 ===
local function clearMountSystem(playerId)
    isFollowing[playerId] = false
    if mountCreature[playerId] then
        World:despawnActor(mountCreature[playerId])
        mountCreature[playerId] = nil
    end
    if mountProjectile[playerId] then
        World:despawnActor(mountProjectile[playerId])
        mountProjectile[playerId] = nil
    end
end



local function clear(event)
    if event.uielement =="7545420593511473091-24919_173" then
        local playerId = event.eventobjid
        clearMountSystem(playerId)
    end
end
ScriptSupportEvent:registerEvent("UI.Button.Click", call)
local function call(event)
    if event.uielement =="7545420593511473091-24919_172" then
        local playerId = event.eventobjid
        clearMountSystem(playerId)
        spawnMountSystem(playerId)
    end
end
ScriptSupportEvent:registerEvent("UI.Button.Click", call)

宠物系统(lua)
作者
站长
发表于
2025-11-24

评论