How can I detect whether or not a part is grounded?

I’m attempting to make a part hop like a player, but I’m having a lot of trouble with figuring out how to detect if the part is grounded or not.

I’ve tried detecting if #Part:GetTouchingParts() == 0, but that didn’t work right for me so I switched to raycasting. I thought this would be my solution, but I’m having difficulty with it.

Here’s a video demonstrating the issue. If I get too far from 0, 0, 0 the raycast no longer detects that the part is grounded.

Here is my current code (inside a loop detecting if space is down)

local ignore = Player.Character:GetDescendants()
table.insert(ignore, Ball)
local grounded, atPosition = workspace:FindPartOnRayWithIgnoreList(Ray.new(Ball.Position, Ball.Position - Vector3.new(0, 10, 0)), ignore)
if grounded then
	print("grounded")
	Ball.Velocity = Vector3.new(Ball.Velocity.X, Ball.Velocity.Y + 50, Ball.Velocity.Z)
end

The second param is the direction, not ending position. Try deleting Ball.Position

1 Like

Also, I am pretty sure findpartonray is deprecated, use raycast instead

Thank you, I didn’t realize! I’ll try changing this right now.

Alright, I’ll keep that in mind :eyes:

I’m stumped. I tried changing the second parameter to the direction, didn’t work. I switched to using raycast instead, and this is my code to detect if the ball is on the ground or not:

local function getPlayerParts()
	local parts = {}
	for _,part in pairs(Player.Character:GetDescendants()) do
		if part:IsA("BasePart") then
			table.insert(parts, part)
		end
	end
	return parts
end

local function grounded()
	local blacklistedParts = getPlayerParts()
	table.insert(blacklistedParts, Ball)
	
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = blacklistedParts
	
	local ray = workspace:Raycast(Ball.Position, Ball.Position - Vector3.new(0, 15, 0), params)
	if ray and ray.Instance then
		return true
	else
		return false
	end
end

it still has the exact same effect as my first try though :frowning:

This might be an XY problem; maybe I should just ask how to make a part jump instead?

You still didn’t remove the Ball.Position in the second parameter

When I did that, it wouldn’t let me jump at all. Are you sure that’s what I need to do?

Its supposed to be a direction not a position so yea, I have no idea why it won’t let you jump tho. Try adding print statements

Turns out you were right, I just did what you had asked wrong. I removed the minus aswell, making the raycast detect 15 studs up high. With the minus in, now my code works. Thank you!

1 Like