Send a raycast to each outer edge of the feet
let chatgpt cook!!!
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local RAYCAST_DISTANCE = 1 -- Adjust this value as needed
local RAYCAST_PARAMS = RaycastParams.new()
RAYCAST_PARAMS.FilterType = Enum.RaycastFilterType.Blacklist
local function GetCorners(part)
local size = part.Size / 2
local cf = part.CFrame
return {
cf * Vector3.new(-size.X, -size.Y, -size.Z),
cf * Vector3.new( size.X, -size.Y, -size.Z),
cf * Vector3.new(-size.X, size.Y, -size.Z),
cf * Vector3.new( size.X, size.Y, -size.Z),
cf * Vector3.new(-size.X, -size.Y, size.Z),
cf * Vector3.new( size.X, -size.Y, size.Z),
cf * Vector3.new(-size.X, size.Y, size.Z),
cf * Vector3.new( size.X, size.Y, size.Z)
}
end
local function GetOuterEdges(part)
local corners = GetCorners(part)
return {
{corners[1], corners[2]}, -- Bottom front
{corners[1], corners[3]}, -- Left front
{corners[2], corners[4]}, -- Right front
{corners[5], corners[6]}, -- Bottom back
{corners[5], corners[7]}, -- Left back
{corners[6], corners[8]} -- Right back
}
end
local function CreateEdgePart(startPos, endPos, parent)
local edge = Instance.new("Part")
edge.Anchored = false
edge.CanCollide = false
edge.Material = Enum.Material.Neon
edge.Color = Color3.new(1, 0, 0) -- Red color for visibility
edge.Size = Vector3.new(0.1, 0.1, (startPos - endPos).Magnitude)
edge.CFrame = CFrame.new((startPos + endPos)/2, endPos)
edge.Parent = parent
local weld = Instance.new("WeldConstraint")
weld.Part0 = edge
weld.Part1 = parent
weld.Parent = edge
return edge
end
local function IsCornerGrounded(corner, character)
RAYCAST_PARAMS.FilterDescendantsInstances = {character}
local raycastResult = workspace:Raycast(corner, Vector3.new(0, -RAYCAST_DISTANCE, 0), RAYCAST_PARAMS)
return raycastResult ~= nil
end
local function VisualizeFootEdges(character)
local leftFoot = character:FindFirstChild("LeftFoot")
local rightFoot = character:FindFirstChild("RightFoot")
if leftFoot and rightFoot then
local leftFootEdges = GetOuterEdges(leftFoot)
local rightFootEdges = GetOuterEdges(rightFoot)
for _, edge in ipairs(leftFootEdges) do
CreateEdgePart(edge[1], edge[2], leftFoot)
end
for _, edge in ipairs(rightFootEdges) do
CreateEdgePart(edge[1], edge[2], rightFoot)
end
end
end
local function CheckGrounded(character, isOnGround)
local leftFoot = character:FindFirstChild("LeftFoot")
local rightFoot = character:FindFirstChild("RightFoot")
if leftFoot and rightFoot then
local leftCorners = GetCorners(leftFoot)
local rightCorners = GetCorners(rightFoot)
local groundedCorners = 0
for _, corner in ipairs(leftCorners) do
if IsCornerGrounded(corner, character) then
groundedCorners = groundedCorners + 1
end
end
for _, corner in ipairs(rightCorners) do
if IsCornerGrounded(corner, character) then
groundedCorners = groundedCorners + 1
end
end
local isGrounded = groundedCorners > 0
if isGrounded then
print('player is on ground')
else
print('player is in air')
end
return isOnGround, groundedCorners
end
return isOnGround, 0
end
local function OnCharacterAdded(character)
VisualizeFootEdges(character)
local isOnGround = false
RunService.Heartbeat:Connect(function()
isOnGround = CheckGrounded(character, isOnGround)
end)
end
local player = Players.LocalPlayer
player.CharacterAdded:Connect(OnCharacterAdded)
if player.Character then
OnCharacterAdded(player.Character)
end