Hello, I have been working on a pet follower module that positions the pet perfectly over the ground but I am struggling with it:
Here is my code for calculating the Y axis:
function petFollower:PositionPet()
local Yoffset = nil
local lookDown = (self.Pet.PrimaryPart.CFrame*CFrame.Angles(math.rad(-90),0,0)).LookVector
local rayCast = workspace:Raycast(self.Pet.PrimaryPart.Position, lookDown * 10)
if rayCast then
Yoffset = rayCast.Position.Y - self.Pet.PrimaryPart.Size.Y/2
local cf = self.Character.PrimaryPart.CFrame * CFrame.new(5, Yoffset, 5)
self.Pet:SetPrimaryPartCFrame(cf)
end
end
Hey there. This is actually a simpler solution than you may think. Simply, you need to factor out the bottom half of the pet, since position is based on the center.
Using addition, increase Yoffset by half of the Y-size of the pet. Here is a modified code:
function petFollower:PositionPet()
local Yoffset = nil
local lookDown = (self.Pet.PrimaryPart.CFrame*CFrame.Angles(math.rad(-90),0,0)).LookVector
local rayCast = workspace:Raycast(self.Pet.PrimaryPart.Position, lookDown * 10)
if rayCast then
Yoffset = rayCast.Position.Y - self.Pet.PrimaryPart.Size.Y/2
local ModelOffset = self.Pet:GetExtentsSize().Y/2
local cf = self.Character.PrimaryPart.CFrame * CFrame.new(5, Yoffset+ModelOffset, 5)
self.Pet:SetPrimaryPartCFrame(cf)
end