How do I make a raycast go to exactly the bottom of the player's feet?

Hello, I’m trying to make an anti fly system in my game and I was told that using rays to find a part below a player is a good idea for a part of that system. I can’t seem to figure out how to get the ray to point right below the feet of the player so it can see if the part is there. I would appreciate help!

2 Likes

You could set the Raycast on the server, then detect if it’s hitting any specific part possibly? :thinking:

local RunService = game:GetService("RunService")
local RaycastCheck = 1

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local HRP = Character:WaitForChild("HumanoidRootPart")

        RunService.Heartbeat:Connect(function()
            local RayOrigin = HRP.Position
            local RayDirection = Vector3.new(0, -100, 0) 

            local Params = RaycastParams.new()
            Params.FilterType = Enum.RaycastFilterType.Blacklist
            Params.FilterDescendantsInstances = {Character}

            local Result = workspace:Raycast(RayOrigin, RayDirection, Params)

            if Result then
                local RayInstance = Result.Instance
                print("Instance hit: "..RayInstance)
                if RayInstance.Name == "Anti-Fly Part" then
                    Character.Humanoid:TakeDamage(100)
                end
            end
        wait(RaycastCheck)
        end)
    end)
end)

Raycasting is prob 1 of the more interesting topics to cover, but it gets the job done it seems like (No clue if this would work)

Using Vector3.new(0, -1, 0) as the direction vector would most likely be better because if you use the down vector of the rootpart, the exploiter can just flip their body upside down or make their down vector never point at “Anti-Fly Part”

1 Like

Vector3.new(0, -1, 0) seems a bit too low for detecting Raycast colissions down below, wouldn’t the Y variable be set as a high negative number?

I meant as the direction unit vector* Obviously you would need to multiply it by like 100 or something to extend it out lower. Also in your edit, the direction shouldn’t include HRP.Position.

local RayOrigin = HRP.Position
local RayDirection = Vector3.new(0, -100, 0) 
1 Like

The issue with this for me is that I’m using wallstick library by Ego Moose (Wall stick/Gravity Controller) so the bottom of the player can be in many directions not always downwards Y so I’m not sure how to find out which way I need to look

Basically the raydirection isnt always 1 down it can be different angles based on the player’s feet

Sorry I should’ve included this in the original post

1 Like

I’m not super experienced with raycasting, although, could you get the position of the player’s feet/legs possibly?