Roblox 项目滞后

Roblox Item is lagging

我添加了一个网格来跟随玩家所到之处。但是当玩家奔跑时网格有点滞后。我知道渲染速度不够快,但是有人知道如何添加网格而不延迟吗?

local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local backpackItemWorkspace = game.ReplicatedStorage.Meshes[pet.Name]:Clone()  
backpackItemWorkspace.Parent = game.Workspace.CurrentPets

RunService.Stepped:Connect(function()
    local location = humanoidRootPart.CFrame
    backpackItemWorkspace.CFrame = location * CFrame.new(2, 2, 3)
end)

使用焊接连接网格和根部分,因此您无需每次都使用 RunService 移动网格。

local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local backpackItemWorkspace = game.ReplicatedStorage.Meshes[pet.Name]:Clone()  
backpackItemWorkspace.Parent = game.Workspace.CurrentPets

function attach(hroot, mesh)
  local weld = Instance.new("WeldConstraint", mesh)
  local location = hroot.CFrame
  mesh.CFrame = location + Vector3.new(2, 2, 3)
  weld.Part0 = hroot
  weld.Part1 = mesh
  return weld
end

attach(humanoidRootPart, backpackItemWorkspace)

-- please comment if it makes any errors

好的,对于那些为养宠物(网眼)而苦苦挣扎的人来说,可以顺畅地跟随您并始终躺在您的背上。我现在已经在这个主题上花了几个小时,终于让它发挥作用了。这就是你的做法:

local character = player.Character
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
    
//where you copy you pet from
local backpackItemWorkspace = game.ReplicatedStorage.Meshes[pet.Name]:Clone()

//where you keep your pets in the workspace  
backpackItemWorkspace.Parent = game.Workspace.CurrentPets

//call the function for attaching the pet
attachPet(backpackItemWorkspace, character, humanoidRootPart) 

function attachPet (pet, char, humanoidRootPart)
   local focusPart = humanoidRootPart
   local newPet = pet
   local z = -5
   local x = 1

   local bodyPos = Instance.new("BodyPosition")
   bodyPos.Parent = newPet
   bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

   local bodyGyro = Instance.new("BodyGyro")
   bodyGyro.Parent = newPet
   bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

   while wait() do
       bodyPos.Position = focusPart.Position + focusPart.CFrame.LookVector * z + focusPart.CFrame.RightVector * x
       bodyGyro.CFrame = focusPart.CFrame
   end
end

此外,当您卸下宠物时,您必须从存放它的地方摧毁它。在我的例子中,我将它们存储在播放器上的一个文件夹中,我称之为 collectionInventory。

local collectionInventory = player:WaitForChild("CollectionInventory")
collectionInventory[petName]:Destroy()

希望这可以避免其他人浪费几个小时进行研究。尽管您可能会从那些“浪费的时间”中学到很多东西;)