Basically I only want to find the floor material; or the material that the player is walking on.
It is simply enough to do humanoid.FloorMaterial, but this cannot work in my case.
I am using Gravity Controller which has the player slightly off of the ground; and on top of that it keeps invisible parts under the player in order to control gravity more easily.
That said I believe I have a way around these roadblocks; however I am not sure if it is possible or what the best way would be to go about this, which is why I am making this post.
Raycasting may work, as I have already used it in my game before; however, I am sure a constant raycast happening would be strain on resources. Either server or client.
I would prefer another option where I could return the material a stud or 2 under the Character’s feet. I believe I am seen this before but cannot remember how it worked or how it was coded.
I believe it was a part below the character and it used Touched to return the information that was needed, in order for more to code to run.
If anyone can get me moving in the right direction, please feel free to give me some insight!
It depends on your use case. If you really need it often then RunService would be fine. I believe there wouldn’t be that much strain. If you only need it when something happens you might want to look at an event.
local TouchPart = Instance.new("Part", workspace)
TouchPart.Anchored = true
TouchPart.CanCollide = false
TouchPart.Size = Vector3.new(1, 1, 1)
TouchPart.Transparency = 1
local TouchConnection = TouchPart.Touched:Connect(function() end)
function GetPartsBelowPlayer(Player)
local Character = Player.Character
if Character then
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
if HumanoidRootPart then
TouchPart.CFrame = HumanoidRootPart.CFrame + Vector3.new(0, -3.5, 0)
return TouchPart:GetTouchingParts()
end
end
end
There’s an issue with this, it casts straight down below the player at all times, not to its orientation. Usually you want to subtract the UpVector times the distance expected.
Raycasting does not cost a lot of memory in usage, it’s unbelievably low-cost and accurate method. Use an appropriate loop for it and it will function seamlessly.
local function GetPartBeneathPlayer(Player, BlacklistArray, Distance)
local Character = Player.Character
if Character then
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
if BlacklistArray == nil then BlacklistArray = {} end
if Distance == nil then Distance = 5 else Distance = math.abs(Distance) end
table.insert(BlacklistArray, Character)
local RayParameters = RaycastParams.new()
RayParameters.FilterDescendantsInstances = BlacklistArray
RayParameters.FilterType = Enum.RaycastFilterType.Blacklist
RayParameters.IgnoreWater = true
local RayOrigin = HumanoidRootPart.Position
local RayDestination = HumanoidRootPart.CFrame + (HumanoidRootPart.CFrame.UpVector * -Distance)
local RayDirection = RayDestination - RayOrigin
local RaycastResult = workspace:Raycast(RayOrigin, RayDirection.Position, RayParameters)
if RaycastResult ~= nil then
if RaycastResult.Instance ~= nil then
return RaycastResult.Instance
end
end
end
end