How to check if the player is on the edge of the building

What you can do, is raycasting. We first tell the script to create an ray, 5 studs in front of the HumanoidRootPart.LookVector. Then we ray downwards 10 studs, to check if the ray found anything, If it did not find anything, we know there is a “long way down”, hints we’re at a edge of a building for example.

local RayStart = HumanoidCFrame.Position + HumanoidCFrame.LookVector*5 -- 5 studs ahead of player
local RayDirection = RayStart + Vector3.new(0,-1,0) -- We just tell the ray to go straight down
local RayResult = workspace:Raycast(RayStart, RayDirection * 10) -- We ray 10 studs downwards
if not RayResult then
-- If no ray result, we know they're on a edge.
end
6 Likes

That’s a bad practice, as an example:

image

It depends of what direction the player is looking at.

Yup, but that raycast when should happen? on a loop? when player is jumping? I think having a touching part at the edges as @bura1414 suggested is a good idea

But isn’t that intended? He wants speed boost before they jump off the building. They wont jump sideways off the building.

1 Like

thats exatcly what i want, to work when going off the the building, thank you so much @TortenSkjold i will just finish my homework and try in studio

2 Likes

He never told that? Just tell me if I’m wrong with a quote.

how can i check if the player is on the edge of an building “a part”?

im trying to make a system to give you an speed boost when you jump off from a build by the edge and i have no idea of how to do it, can someone help me?

like

1 Like

“im trying to make a system to give you an speed boost when you jump off from a build by the edge and i have no idea of how to do it, can someone help me?”

1 Like

Okay, if you want to include player’s direction your problem is solved.

You could do this but cast a ton of rays in a circle around the character and if its no longer hitting the roof then it’s on the edge.

Oh my gosh im sorry i didnt fully get what you meant, my way would work but @TortenSkjold way is probably more viable

1 Like

There is no need to cast a ton of rays. Personally I don’t see that it’s worth having the feature, that they get speed boost while jumping sideways or backwards. :slight_smile:

2 Likes

1 Like

its boosting without being at edge whats happening?

 hum.Jumping:connect(function()
	local RayStart = hrp.Position + hrp.CFrame.LookVector *5-- 5 studs ahead of player
	local RayDirection = RayStart + Vector3.new(0,-1,0) -- We just tell the ray to go straight down
	local RayResult = workspace:Raycast(RayStart, RayDirection * 10) -- We ray 10 studs downwards
	
	if not RayResult and hum.FloorMaterial ~= Enum.Material.Air then
		local Vel = Instance.new("BodyVelocity")
		local n = math.random(5,10)
		Vel.Parent = hrp
		Vel.Velocity = Vector3.new(0,0,0)
		Vel.MaxForce = Vector3.new(1,1,1) * math.huge
		Vel.Velocity = hrp.CFrame.LookVector * 50 + Vector3.new(0,10,0)
		print("bost")
		
		game.Debris:AddItem(Vel, .15)
	end
end)

Have you tried increasing the 10 to 20?

yes, but its boosting with only floor in front of me

Here you go, the issue was that RayDirection bugged. Instead we just get the upvector, and goes into the negative by 1.

		local RayStart = hrp.Position + hrp.CFrame.LookVector * 5-- 5 studs ahead of player
		local RayDirection = -hrp.CFrame.UpVector*1 -- We just tell the ray to go straight down
		local RayResult = workspace:Raycast(RayStart, RayDirection * 10) -- We ray 10 studs downwards
		print(RayResult)
		if not RayResult and hum.FloorMaterial ~= Enum.Material.Air then
			local Vel = Instance.new("BodyVelocity")
			local n = math.random(5,10)
			Vel.Parent = hrp
			Vel.Velocity = Vector3.new(0,0,0)
			Vel.MaxForce = Vector3.new(1,1,1) * math.huge
			Vel.Velocity = hrp.CFrame.LookVector * 50 + Vector3.new(0,10,0)
			print("bost")

			game.Debris:AddItem(Vel, .15)
		end
	end
3 Likes

its working now, thank you so much, god bless u

1 Like

Be aware, that the orientation of HumanoidRootPart changes, when the character falls over, is swimming ect.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.