How to get part to slide along ground

I’m having trouble trying to get a part to spawn in, and then follow the character, but keep it along the ground (not floating or inside the ground)

Here’s what it currently looks like:


When I spawn it in, it goes into the ground, and when I move, it moves up and stays in the air. The red part is the HumanoidRootPart.

Server code to place the item in

local function Equip(player, pet)
	-- Check for pet in folder
	local EquippedPet = Pets:FindFirstChild(pet)
	if not EquippedPet then return end
	
	--/// EDIT Check to make sure player owns pet
	
	-- Make sure player doesn't already have a pet equipped
	if PetContainer:FindFirstChild(player.Name) then
		-- If they do, unequip their current pet
		Unequip(player, pet)
	end
	
	-- Clone pet
	local ClonedPet = EquippedPet:Clone()
	ClonedPet.Name = player.Name
	
	-- Set position
	ClonedPet:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(5, 0, 0))
	
	-- Parent
	ClonedPet.Parent = PetContainer
	
	-- Set Network owner
	ClonedPet.PrimaryPart:SetNetworkOwner(player)
end

Client code to move it

-- Render and make pet follow players
local function RenderPet(pet)
	-- Get the TargetPlayer
	local TargetPlayer = Players[pet.Name]
	if not TargetPlayer then return end
	
	-- Get the TargetCharacter
	local TargetCharacter = TargetPlayer.Character
	if not TargetCharacter then return end
	
	-- Wait for PrimaryPart
	pet:WaitForChild('HumanoidRootPart')
	
	-- Get Body's	
	local BodyGyro = pet.HumanoidRootPart.BodyGyro
	local BodyPosition = pet.HumanoidRootPart.BodyPosition
	
	local function MovePet()
		-- Check if player is 15 studs away from pet
		local Distance = (TargetCharacter.HumanoidRootPart.Position - pet.HumanoidRootPart.Position).Magnitude
		if Distance < 15 then return end
		
		-- If so, we want to keep moving the pet, until the player has come to a complete stop
		while TargetCharacter.Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) do
			BodyGyro.CFrame = TargetCharacter.HumanoidRootPart.CFrame
			BodyPosition.Position = CFrame.new(TargetCharacter.HumanoidRootPart.CFrame * Vector3.new(5, 0, 0)).Position
			wait()
		end
	end
	
	RunService.RenderStepped:Connect(MovePet)
end

So ye, I just want it to stay along the ground. Originally I was gonna do like ‘-2’ from the HRP, but that would only work for players who haven’t edited their heights. I need it to always be along the ground, irrelevant of player heights

1 Like

You could always raycast below the pet with a certain distance for it to slide along the ground. And if you don’t want it going in the ground, just offset it above the ground a few studs after getting the raycast hit position.

Not sure if I’m doing something wrong, but it still does it for creating the model. I don’t know if the BodyPosition might be causing problems, as the model spawns, and then moves to another positions (video in the post shows it)

-- Set position
local CheckForFloor = Ray.new(player.Character.HumanoidRootPart.Position, Vector3.new(0, -20, 0))
local Floor, FloorPos = workspace:FindPartOnRay(CheckForFloor)

ClonedPet:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(5, FloorPos.Y, 0))

Raycast down from the players root part and then use the surface normal the cast returns to align the part with the ground.

1 Like
local cframe = HumanoidRootPart.CFrame
local ray = Ray.new(cframe.Position, Vector3.new(0, -20, 0))
local Part, position, normal = workspace:FindPartOnRay(ray, Character, true, false)


ClonedPet:SetPrimaryPartCFrame(CFrame.new(cframe.Position + Vector3Offset, cframe.Position - normal))
1 Like