So I’ve been trying to get the movmemnt for the pet system I’m working on as smooth as possible. I’m currently using the server for moving the pet. If I add a wait for the length of the tween it becomes a lot less bouncy, but it still isn’t as smooth as I’d like it to be.
Example of “bouncy”:
Code:
function follow(plr,obj)
local prev = Vector3.new(0,0,0)
while plr.Character.HumanoidRootPart ~= nil do
wait()
if plr.Character.HumanoidRootPart.Position.X > prev.X + 1 or plr.Character.HumanoidRootPart.Position.X < prev.X - 1 or plr.Character.HumanoidRootPart.Position.Y > prev.Y + 1 or plr.Character.HumanoidRootPart.Position.Y < prev.Y -1 or plr.Character.HumanoidRootPart.Position.Z > prev.Z + 1 or plr.Character.HumanoidRootPart.Position.Z < prev.Z - 1 then
prev = plr.Character.HumanoidRootPart.Position
print("Hello")
local g = {}
local calc = CFrame.new(plr.Character.HumanoidRootPart.CFrame.X,0,plr.Character.HumanoidRootPart.CFrame.Z).p
local goalback = CFrame.new(obj.CFrame.X, 0,obj.CFrame.Z).p)
g.Position = (plr.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,5)).p
g.CFrame = CFrame.new(calc, goalback)
local ti = TweenInfo.new(0.25)
local tween = game:GetService("TweenService"):Create(obj,ti,g)
tween:Play()
end
end
end
Any ideas of how I could achieve the pet movement being smooth?
function givePet (player)
if player then
local character = player.Character
if character then
local humRootPart = character.HumanoidRootPart
local newPet = pet:Clone ()
newPet.Parent = character
local bodyPos = Instance.new(“BodyPosition”, newPet)
bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
local bodyGyro = Instance.new(“BodyGyro”, newPet)
bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
while wait() do
bodyPos.Position = humRootPart.Position + Vector3.new(2, 2, 3)
bodyGyro.CFrame = humRootPart.CFrame
end
end
end
end
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local somePet = ReplicatedStorage.Pet
local alignPositionTemplate = Instance.new("AlignPosition")
alignPositionTemplate.RigidityEnabled = true
local alignOrientationTemplate = Instance.new("AlignOrientation")
alignPositionTemplate.RigidityEnabled = true
-- configure the template properties to your needs, making it smoother and whatnot
local function playerAdded(player)
local function characterAdded(character)
local pet = somePet:Clone()
local charPrimary = character.PrimaryPart
local petPrimary = pet.PrimaryPart
local charOriAttachment = Instance.new("Attachment", charPrimary)
local charPosAttachment = Instance.new("Attachment", charPrimary)
charPosAttachment.Position = Vector3.new(2, 1, 2) -- position offset
local petOriAttachment = Instance.new("Attachment", petPrimary)
local petPosAttachment = Instance.new("Attachment", petPrimary)
local alignPosition = alignPositionTemplate:Clone()
alignPosition.Attachment0 = petPosAttachment
alignPosition.Attachment1 = charPosAttachment
alignPosition.Parent = petPrimary
local alignOrientation = alignOrientationTemplate:Clone()
alignOrientation.Attachment0 = petOriAttachment
alignOrientation.Attachment1 = charOriAttachment
alignOrientation.Parent = petPrimary
pet.Parent = workspace
petPrimary:GetNetworkOwner(player)
-- set the network owner to the player, this ensures that the movement is smooth
end
characterAdded(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:Connect(characterAdded)
end
Players.PlayerAdded:Connect(playerAdded)
With this method, you only need to position the charPosAttachment whenever you to change the offset, so if the pet is simple and you only want it to stay behind the player and not move to other locations you only need to set the offset once
it’s far easier and more efficient than having to update the position and orientation in a while loop
Notes: I made this in a rush, please point out any issues or questions, I also only used the RigidityEnabled property in order to test quickly, in actual use you will want to tweak around with the other properties of the aligns and not use RigidyEnabled (unless you want it to be rigid)
I’ve decided to go with @Strongjohnfgamer 's solution to the problem, This gives me a smooth animation where as with Attachments I don’t get any animations. However, It does have to be said that Attachments would stop one from having to use coroutines. Thanks everyone for your help!
Edit: At some point I might let the client handle this as well @Protori