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)