如何使物体跟随玩家平滑
How to make object following player smooth
我使用 MoveTo 跟踪位置并更改 CFrame 进行旋转,但它看起来非常锯齿。我认为问题是代码在服务器端而不是在客户端,因为我希望其他玩家能够看到跟随玩家的对象
尝试使用 weldConstrain。您可以通过代码创建它并将 2 个部分连接到它。这些零件将彼此保持相同的相对位置。这是关于该主题的 API 参考的 link:https://developer.roblox.com/en-us/api-reference/class/WeldConstraint
这是一个示例代码,在玩家加入游戏时在其头顶添加一个部件:
local Players = game:GetService('Players') -- get's the players service
local function OnPlayerAdded(player)
local PlayerCharacter = player.Character
-- waits until the player's character loads
while PlayerCharacter == nil do
wait(1)
PlayerCharacter = player.Character
end
-- Get's the player's model in workspace
local PlayerModel = workspace:WaitForChild(PlayerCharacter.Name)
-- Get's the player's head inside it's model
local PlayerHead = PlayerModel:WaitForChild('Head')
-- Write the path to the part you want to follow the player, bellow
local FollowPartOriginal = workspace:WaitForChild('Part')
-- If you want to clone the part, so more players can be followed by it,
-- don't delete the line below, if you want only 1 part, delete the line below.
local FollowPart = FollowPartOriginal:Clone()
-- set's the part position on top of the player's head, with an offset
FollowPart.Position = PlayerHead.CFrame.p + Vector3.new(0,2,0)
FollowPart.Parent = workspace
-- creates a weldconstrain
local WeldConstraint = Instance.new('WeldConstraint')
-- connect's the weld to the player's head and the other part
WeldConstraint.Part0 = PlayerHead
WeldConstraint.Part1 = FollowPart
WeldConstraint.Parent = PlayerHead
end
Players.PlayerAdded:Connect(OnPlayerAdded)
将这段代码放在脚本中(不是本地脚本)。
如果单独使用该功能,请不要忘记放置播放器参数。参数需要包含播放器Instance(ex: game.Players.PlayerName)
要更改零件位置,请使用此代码行中的偏移量:
FollowPart.Position = PlayerHead.CFrame.p + Vector3.new(write offset here)
如果你想让零件停止跟随玩家,制作脚本删除零件,或者焊接约束(玩家头部的child)
我使用 MoveTo 跟踪位置并更改 CFrame 进行旋转,但它看起来非常锯齿。我认为问题是代码在服务器端而不是在客户端,因为我希望其他玩家能够看到跟随玩家的对象
尝试使用 weldConstrain。您可以通过代码创建它并将 2 个部分连接到它。这些零件将彼此保持相同的相对位置。这是关于该主题的 API 参考的 link:https://developer.roblox.com/en-us/api-reference/class/WeldConstraint 这是一个示例代码,在玩家加入游戏时在其头顶添加一个部件:
local Players = game:GetService('Players') -- get's the players service
local function OnPlayerAdded(player)
local PlayerCharacter = player.Character
-- waits until the player's character loads
while PlayerCharacter == nil do
wait(1)
PlayerCharacter = player.Character
end
-- Get's the player's model in workspace
local PlayerModel = workspace:WaitForChild(PlayerCharacter.Name)
-- Get's the player's head inside it's model
local PlayerHead = PlayerModel:WaitForChild('Head')
-- Write the path to the part you want to follow the player, bellow
local FollowPartOriginal = workspace:WaitForChild('Part')
-- If you want to clone the part, so more players can be followed by it,
-- don't delete the line below, if you want only 1 part, delete the line below.
local FollowPart = FollowPartOriginal:Clone()
-- set's the part position on top of the player's head, with an offset
FollowPart.Position = PlayerHead.CFrame.p + Vector3.new(0,2,0)
FollowPart.Parent = workspace
-- creates a weldconstrain
local WeldConstraint = Instance.new('WeldConstraint')
-- connect's the weld to the player's head and the other part
WeldConstraint.Part0 = PlayerHead
WeldConstraint.Part1 = FollowPart
WeldConstraint.Parent = PlayerHead
end
Players.PlayerAdded:Connect(OnPlayerAdded)
将这段代码放在脚本中(不是本地脚本)。 如果单独使用该功能,请不要忘记放置播放器参数。参数需要包含播放器Instance(ex: game.Players.PlayerName) 要更改零件位置,请使用此代码行中的偏移量:
FollowPart.Position = PlayerHead.CFrame.p + Vector3.new(write offset here)
如果你想让零件停止跟随玩家,制作脚本删除零件,或者焊接约束(玩家头部的child)