Pet floats next to player when player walks on a ramp

I’m making a pet follow script where the pet follows the player next to them on the ground. I’ve done this with an AlignPosition. Everything works fine until the player goes on a ramp of the pet and the player aren’t at the same height.


Here the pet should be on the ground, not floating next to the player. How can I achieve this?

Here is the code:

local alignPosition ="AlignPosition")
alignPosition.Responsiveness = 100
alignPosition.Parent = pet.PrimaryPart

local attachment1 ="Attachment")
local attachment2 ="Attachment")

attachment1.Parent = pet.PrimaryPart

attachment2.Parent = player.Character:WaitForChild("HumanoidRootPart")
attachment2.CFrame = petCFrameOffset -, pet.PrimaryPart.Size.Y / 2, 0)

alignPosition.Attachment0 = attachment1
alignPosition.Attachment1 = attachment2

local alignOrientation ="AlignOrientation")
alignOrientation.Responsiveness = 100
alignOrientation.RigidityEnabled = true
alignOrientation.Parent = pet.PrimaryPart
alignOrientation.Attachment0 = attachment1
alignOrientation.Attachment1 = attachment2

pet.PrimaryPart.CollisionGroup = "Pets"

pet.Parent = game.Workspace

cast a ray beneath the pet and then move the pet to the ray

1 Like

or something like this:

local at2cf = petCFrameOffset -, pet.PrimaryPart.Size.Y / 2, 0)
local ray =,,-math.huge,0)
local _,position,_ = workspace:FindPartOnRay(ray,pet)
attachment2.CFrame =,at2cf.Position.Y-position.Y,0)

I tried this but it just sets attachment2’s Y position to -inf

1 Like

wait what please give me video of it and if you actually are standing on a surface. (or just replace local _,position,_ = workspace:FindPartOnRay(ray,pet) with local _,position,_ = workspace:FindPartOnRay(ray)

1 Like

This is what it looks like. The Bird Model gets destroyed because it gets moved to the void

1 Like

remove the pet argument as i said above.

1 Like

I tried that but nothing changed

1 Like

well wow that is quite weird. maybe try limiting the ray? replace math.huge with some smaller number like 10 or 100 and are you sure that the position i thought is the position of the pet correct?

1 Like

Maybe add a restraint on the pets Y value.

1 Like

How do I put a restraint on the pets y axis?

One added line should do it …
if Y >= 100 then Y = 100 end

“100” being the highest you wish the pet to move upwards

You may want to take a look at how this is done: Not sure if that is the right one.

Just so I’m not sending you down a rabbit hole …

local part = script.Parent
local maximumValue = 10 --> The highest it is allowed to move.

-- Connect the function to the part's "Changed" event
    if property == "Position" then
        local currentPosition = part.Position
        if currentPosition.Y > maximumValue then
            currentPosition =, maximumValue, currentPosition.Z)

        part.Position = currentPosition

The first solution that @Qinrir said is correct. but the example is not.

Instead of checking what part the ray touched, you should check what Y level the ray is at upon touch.

local raycastparams =
raycastparams.FilterDescendantsInstances = {character, pet}
raycastparams.FilterType = Enum.RaycastFilterType.Exclude

local origin = pet.Position
local ray = workspace:Raycast(origin,, -10000, 0), raycastparams)

local YLevel = ray.Position.Y

YLevel, hence the name, is the Y level that the ray touched at. You should also include the pet’s origin height to the ground before changing the y level to the ray’s y level.

1 Like

This looks really clean and would stop the height before it was even a problem. Well done, :heart:’ed
Both attempts are limiting the Y height and that would be a way to fix this …

1 Like

I tried this but the ray is always nil. How do I fix this?

That just means the ray points to the void.

But even when I’m walking on an elevated part, the ray is still nil

Okay, I found it. math.huge is too big so the ray kind of glitches out. Change it to something like -10000 and see if it works.

This works but I don’t know how to get this to work with AlignPosition

Nevermind, I got that to work. Thanks for all the help!