Calculating the Y offset for a pet following system

Hello, I have been working on a pet following system but I have been struggling with calculating the Y offset so that the pet “Walks” on the ground.
My code:

		local position = (character.HumanoidRootPart.CFrame * CFrame.new(x,1,z))
		
		pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(position,0.1))

Thank you!

local _, characterSize = character:GetBoundingBox()
local _, petSize = pet:GetBoundingBox()

local offsetY = - characterSize.Y/2 + petSize.Y/2

pet:SetPrimaryPartCFrame(character.PrimaryPart.CFrame * CFrame.new(offsetX, offsetY, offsetZ))

I don’t think pets in games even “walk” anyways, but if you want them to actually walk you can have your pet be a “humanoid” and have it walk on the ground with physics

1 Like

Thank you very much! It works like a charm! I have one question, how do I get it to work with a sine wave without it going through the ground?

		pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY+math.sin(time()*3), z),0.1))

math.sin(n) will return a value between 1 and -1. You want everything to be positive, so you add 1 to it.

1 Like

I tried this:

pet:SetPrimaryPartCFrame(pet.PrimaryPart.CFrame:Lerp(character.PrimaryPart.CFrame * CFrame.new(x, offsetY+(math.sin(time()*3)+1), z),0.1))

But there is still a tiny offset.