How would I make this pet remain on the floor?

How would I make this pet remain on the floor indefinitely? I’ve attempted to use RayCast to detect flooring and it works to an extent - However, seeing from below you can see how it enters wedge parts etc. etc.

Code:
local TweenService = game:GetService("TweenService")

local Player = game:GetService("Players").LocalPlayer
repeat wait(.05) until Player.Character ~= nil
local Character = Player.Character

local PetObj = Player:WaitForChild("isPet").Value

local FollowTween, TurnTween

local Distance = 5

function UpdateTween()
	local Tweeninfo = TweenInfo.new(.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
	
	local HeightRay = Ray.new(Character["HumanoidRootPart"].Position, Character["HumanoidRootPart"].CFrame.UpVector * -300)
	local GetPartsOnRay, Position = game:GetService("Workspace"):FindPartOnRayWithIgnoreList(HeightRay, {Character, PetObj})
	
	if GetPartsOnRay then
	FollowTween = TweenService:Create(PetObj["Base"]["BodyPosition"], Tweeninfo, {Position = Vector3.new(Character["HumanoidRootPart"].Position.X, Position.Y, Character["HumanoidRootPart"].Position.Z) - Character["HumanoidRootPart"].CFrame.LookVector * Distance})
	end
	TurnTween = TweenService:Create(PetObj["Base"]["BodyGyro"], Tweeninfo, {CFrame = CFrame.new(PetObj["Base"].Position, Vector3.new(Character["HumanoidRootPart"].Position.X, PetObj["Base"].Position.Y, Character["HumanoidRootPart"].Position.Z))})
end

game:GetService("RunService").Stepped:Connect(function()
	UpdateTween()
	TurnTween:Play()
	
	--[[if (PetObj["Base"].Velocity).Magnitude > 0.5 then
		print("Moving")
	else
		print("Idle")
	end]]--
	
	if (Character["HumanoidRootPart"].Velocity).Magnitude > 2.5 then
		if (Character["HumanoidRootPart"].Position - PetObj["Base"].Position).Magnitude > Distance+Distance/2 then
			FollowTween:Play()
		end
	end
end)

https://gyazo.com/1224dff61aae08b9838a4307d5c08ff5

Let me get this right, you want the pet to have the same angle as the floor?

1 Like

Yes? I want it to avoid going through parts as seen in the WedgePart.

Okay, so one way you could do that is by setting the Y element of the position manually (every render step) and tweening X and Z, though that might look a little choppy when a user falls or jumps over a hole.

The way it currently works is by RayCasting downwarda and getting that position - As the game will have different ground levels it would be inefficient to restrict the Y axis.

Also, this doesn’t help with WedgeParts neither.