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?
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)
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?
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.
This looks really clean and would stop the height before it was even a problem. Well done, ’ed
Both attempts are limiting the Y height and that would be a way to fix this …