Pet floats next to player when player walks on a ramp

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

1 Like

or something like this:

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

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.
mathclamp-explanation

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
part.Changed:Connect(function(property)
    if property == "Position" then
        local currentPosition = part.Position
       
        if currentPosition.Y > maximumValue then
            currentPosition = Vector3.new(currentPosition.X, maximumValue, currentPosition.Z)
        end

        part.Position = currentPosition
    end
end)

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.new()
raycastparams.FilterDescendantsInstances = {character, pet}
raycastparams.FilterType = Enum.RaycastFilterType.Exclude

local origin = pet.Position
local ray = workspace:Raycast(origin, Vector3.new(0, -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!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.