Hello I have been working on a pet humanoid follower and I have some issue the pet doesn’t go behind the player
Here is the code :
RunService.RenderStepped:Connect(function(deltaTime)
for _ , folder in PlayerPets:GetChildren() do
for _ , pet in folder:GetChildren() do
local PlayerName = folder.Name
local Character = workspace:FindFirstChild(PlayerName)
for _, descendant in pairs(pet:GetDescendants()) do
if descendant:IsA("MeshPart") or descendant:IsA("BasePart") then
descendant.CanCollide = false
end
end
if Character == nil or not Character:FindFirstChild("HumanoidRootPart") then continue end
local rayOrigin = Vector3.new(pet.PrimaryPart.Position.X, pet.PrimaryPart.Position.Y + 20 , pet.PrimaryPart.Position.Z)
local params = RaycastParams.new()
params.FilterDescendantsInstances = {pet}
params.FilterType = Enum.RaycastFilterType.Blacklist
local direction = Vector3.new(0, -1, 0) * 100
local ray = workspace:Raycast(rayOrigin, direction, params)
local FinalCFrame
if ray then
FinalCFrame = CFrame.new(Character.HumanoidRootPart.CFrame.X - 5 , ray.Position.Y + pet.PrimaryPart.Size.Y * 1.5 , Character.HumanoidRootPart.CFrame.Z + 5)
end
local GoalCF = Character.HumanoidRootPart.CFrame
GoalCF = GoalCF * CFrame.Angles(0,math.rad(0),0)
GoalCF = (GoalCF - GoalCF.LookVector * 5)
GoalCF = GoalCF * CFrame.Angles(0 , -math.rad(0),0)
pet.PrimaryPart.AlignPosition.Position = FinalCFrame.Position
--pet.PrimaryPart.AlignPosition.Position = GoalCF.Position
pet.PrimaryPart.AlignOrientation.CFrame = GoalCF
end
end
end)