How do i optimize my raycasting

Hi im trying to use a raycast to detect when the player is on the ground so i can code in some features in future but after a bit of the code running there is extreme slowdown and i dont know how to fix it

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Board.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local direction = Vector3.new(0,-1,0)
game:GetService("RunService").Heartbeat:Connect(function()
	local rcresult = workspace:Raycast(Board.Position,direction,raycastParams)
	if rcresult then
		local hp = rcresult.Instance
		if hp then
			print("onground")
		end
		if not hp then
			print("offground")
		end
	end
end)


i tried using Destroy() on the rays but that isnt a property of them :\

2 Likes

Instead of casting a ray every frame, try casting one only when the character’s RootPart changes. And to further optimize, you could set up a debounce.

do
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Board.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local direction = Vector3.new(0,-1,0)

local enabled = true
yourCharacter.HumanoidRootPart:GetPropertyChangedSignal("Position"):Connect(function()
    if enabled then
    enabled = false
	local rcresult = workspace:Raycast(Board.Position,direction,raycastParams)
	if rcresult then
		local hp = rcresult.Instance
		if hp then
			print("onground")
		else
			print("offground")
		end
	end
    task.wait(1) --// There may be a better way to do this, but this would work
    enabled = true
    end
end)
end
1 Like

I think you should make the debounce around .1 instead of a whole second

1 Like

You’re right, I just decided to make it 1 to show that you should put a number in between, otherwise it would be the same as using heartbeat since task.wait() runs using heartbeat

1 Like

This could be due to the excessive amount of prints, try removing them.

3 Likes

I found that the GetPropertyChangedSignal on Position or CFrame doesn’t work. Have you tested this script?

it doesn’t work when u move an object with physics

1 Like

I have not. You may be right as I don’t use GetPropertyChangedSignal often enough to know, but it would still be a better practice to only raycast when the character moves if possible.

try your original code but put an if statement that checks if the last position of the rootpart is 0.2 more magnitude than the current position. this is a good substitute for GetPropertyChangedSignal(“Position”)

2 Likes

I’m not the OP lol @krenzie is.

crap he isnt replying so i thought you were lol my bad

1 Like

Sorry about that i was asleep! Timezones ay? Anyway would i do that something like this?

	local lastpos = Character.Part.Magnitude
	wait(.2)
	local currentpos = Character.Part.Magnitude
	if currentpos - lastpos >= 0.2 then

Ok i think i got it to work how i like thank you! here is the code:

while wait() do
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Board.Parent.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local direction = Vector3.new(0,-1,0)

	local enabled = true
	local currentpos = Character.HumanoidRootPart.Position.Magnitude
	wait(.2)
	local lastpos = currentpos
	wait()
	local currentpos = Character.HumanoidRootPart.Position.Magnitude
	if currentpos - lastpos >= 0.2 or currentpos - lastpos <= -0.2 then
		if enabled then
			enabled = false
			local rcresult = workspace:Raycast(Board.Position,direction,raycastParams)
			if rcresult then
				local hp = rcresult.Instance
				if hp then
					print("onground")
				else
					print("offground")
				end
			end
			task.wait(.1)
			enabled = true
		end
	end
end
1 Like

Using delays and position thresholds is bad for this use case. You want your game to respond instantly when your character leaves the ground, right? Those optimizations will break that.

The optimizations are also unnecessary. You are not slowing down your game by casting one 1-length ray every frame. Even if you were, casting it after x time wouldn’t help because you’d still be excessively slow whenever it casts.

The prints are the culprit of any timing issues in the code you posted. Printing can take a relatively long time, and when you do it every frame you can really slow down your game. Remove them and see how your game performs.

1 Like

Oh damn thanks for that! i had no clue that print was so performance heavy, I had a problem though with all the scripts being that it wont print the “offground” stuff and it would only just stop the onground print from printing so, you would see that your off the ground by the delay in the next onground print but offground wont actually print

1 Like

and they dont see to change a bool value either it seems