I have created a script that makes part ("Pet1) Follow humanoid root part but the issue I am having are two things:
One: how can I make it look a lot smoother (maybe I’m following the wrong part of the character or maybe I should use a different technique
Two: How can I make the part stay parallel to player (so rotations still work)
Here is what happens so far:
--
local Humanoid = script.Parent.HumanoidRootPart
local distance = 5
local Pet1 = workspace.Pets.Pet1
local Follower = Humanoid
while true do
Pet1.CFrame = Follower.CFrame*CFrame.new(0, 0, distance)
Pet1.CFrame = CFrame.new(Pet1.CFrame.X, 1, Pet1.CFrame.Z)
wait()
end
Yes you can weld the parts together but I would always like the part to be sitting on the ground, I have gone with the method of keeping all the ground one level so I can always set it to y=1 but if there is another better method please say
EDIT:
Working smoothly just trying to get it to rotate
CFrame contains Rotation and Position information, so it will be parallel to humanoidrootpart if you type:
Part.CFrame = HumanoidRootPart.CFrame * Offset
-- Lerp Example
for i=0,1,0.01 do -- or 0.02
Part.CFrame = Part.CFrame:Lerp(HumanoidRootPart.CFrame * Offset,i)
end
You may change offset to make it behind character, not inside a humanoidrootpart.
Thanks for the advice to use Heartbeat, it’s making it move perfectly smooth now I’m just doing the rotation now (the code I use for that was):
local Humanoid = script.Parent.HumanoidRootPart
local distance = 5
local Pet1 = workspace.Pets.Pet1
local Follower = Humanoid
local RunService = game:GetService("RunService")
while true do
RunService.Heartbeat:Connect(function()
Pet1.CFrame = Follower.CFrame*CFrame.new(0, 0, distance)
Pet1.CFrame = CFrame.new(Pet1.CFrame.X, 1, Pet1.CFrame.Z)
wait()
end)
wait()
end
I’ve gone through them, CFrames still confuse me, I can make rotations work by removing the second line within the function that sets the Y value. How could I just set the Y value but not change any other values?
You should not use a while loop for this, attachments are pretty good and you can keep the rotation too!
AlvinBlox made an amazing tutorial explaining them.
Edit:
Heartbeat is a loop itself, you don’t need to put it inside a loop.