How do I make a raycast go to bottom (with wall stick/gravity controller)

So I’ve seen 2 posts about that but they didn’t get an response:

I have situation the one that @winet have, i have wallstick/gravity controller script it means that player downwards will not be always Y axis and i need to raycast from player’s feet, how would i make that?

If it’s not guaranteed that down will be equal to the -Y axis, then here’s what you’ll need to do to always raycast downwards from a character’s foot:

local origin = foot.Position

local raycastResult = workspace:Raycast(origin, origin + -foot.CFrame.UpVector * DISTANCE)

The foot variable will need to be the BasePart that’s acting as the character’s foot, and the DISTANCE variable will need to be a number value that determines the maximum length of the raycast (as an example, 8 for eight studs downwards)

can i use humanoidrootpart instead?

Sure, all you’ll need to do is replace:

local origin = foot.Position

with:

local origin = humanoidRootPart.Position

and it should still work as you’d like it to :slight_smile::+1:

for some reasons it doesn’t work

			local origin = Player.Character:WaitForChild("HumanoidRootPart").Position

			local raycastResult = workspace:Raycast(origin, origin + -Player.Character:WaitForChild("HumanoidRootPart").CFrame.UpVector * 5)
			print("Position:", raycastResult.Position)

error: attempt to index nil with ‘Position’

Player.Character has a chance to be nil since characters take time to load for both LocalScripts and server Scripts, so you’ll need to wait for it by doing this:

local character = Player.Character or Player.CharacterAdded:Wait()

local rootPart = character:WaitForChild("HumanoidRootPart")

local origin = rootPart.Position

local raycastResult = workspace:Raycast(origin, origin + -rootPart.CFrame.UpVector * 5)
1 Like

i mean it works but not the way it should
TESTIMAGE2

it should go under player (green dot is the raycast position result)

At the moment, only 1 raycast is being fired, so if the character’s still in the air when the raycast fires then the part won’t be on the floor. The raycast will need to be fired inside of a loop like so:

local RunService = game:GetService("RunService")

local character = Player.Character or Player.CharacterAdded:Wait()

local rootPart = character:WaitForChild("HumanoidRootPart")

RunService.PostSimulation:Connect(function()
	local origin = rootPart.Position

	local raycastResult = workspace:Raycast(origin, origin + -rootPart.CFrame.UpVector * 5)

	if raycastResult then
		print("Position:", raycastResult.Position)
	end
end)

no this is not the problem.
here like how it looks like


and it faces this way for some reasons while it should go down the player, and it prints attempt to index nil with ‘position’ because there is no end in this raycast (because it goes the wrong way, it should go under player not in an axis or in front. it should go under player)
and here how it should look like:

2nd example

The formula should have worked correctly, but I was able to replicate the issues you’re experiencing

Currently working on a fix, but I’ll need a bit of time to test to make sure that it will fire straight down


If I set the value of DISTANCE to 256 instead of 5, the part is being positioned correctly below the character. It seems there might be some issues with raycast accuracy for small distances


@K0nanVarvar2 Here’s a place file with the now-working code (In a server Script inside of ServerScriptService): PartRaycastExample.rbxl (53.4 KB)

The main difference are that I’m using 256 instead of 5 as mentioned in my previous edit, and the addition of RaycastParams to filter out the player’s own character

1 Like

create a vector by writing HumanoidRootPart.Position - Head.Position, then multiply that vector by whatever scalar value you want. that should return you a downwards vector regardless of your current orientation

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