Raycast refusing to cast to position thats underneath user

Making this topic simple.

I’m trying to cast a ray that starts from the HumanoidRootPart and ends Directly underneath the HumanoidRootPart. So hypothetically saying I would need the direction of the ray to move towards the a position using this line: HumanoidRootPart.Position - Vector3.new(0,50,0).

From first expectations, it should atleast cast the ray DIRECTLY UNDERNEATH the character, hitting the floor. But I get this as a result instead:

There’s a possibility that I’m doing this incorrectly but, here’s the line of code:

local Raycast = workspace:Raycast(HRP.Position,HRP.Position - Vector3.new(0,50,0), RayParam)

local Distance = (HRP.Position - Vector3.new(0,50,0)).magnitude
	local Laser = Instance.new("Part")
	Laser.Name = "RayPart"
	Laser.BrickColor = BrickColor.new("Really red")
	Laser.Material = Enum.Material.ForceField
	Laser.Transparency = 0
	Laser.Anchored = true
	Laser.CanCollide = false
	Laser.TopSurface = Enum.SurfaceType.Smooth
	Laser.BottomSurface = Enum.SurfaceType.Smooth
	Laser.formFactor = Enum.FormFactor.Custom
	Laser.Size = Vector3.new(.1, .1, Distance)
	Laser.CFrame = CFrame.new(HRP.Position, Raycast.Position) * CFrame.new(0, 0, -Distance/2)
	Laser.Parent = game.Workspace.Debris
	game.Debris:AddItem(Laser,1)

Parameters

Name Type Default Description
### origin Vector3 The origin point of the ray.
### direction Vector3 The directional vector of the ray. Note that the length of this vector matters, as parts/terrain further away than its length will not be tested.

It’s looking like your math is off for direction (Assuming that HRP IS the correct Origin)

So, this may work

local Raycast = workspace:Raycast(HRP.Position, Vector3.new(0,-50,0), RayParam)

Direction is just a Relative direction and a length, so it has no relationship to the Absolute Position of root part.
I’m hoping…

2 Likes
local Raycast = workspace:Raycast(HRP.Position,-HRP.Position - Vector3.new(0,50,0), RayParam) -- gets the oppisite of the hrp pos. 

Switch

workspace:Raycast(HRP.Position,HRP.Position - Vector3.new(0,50,0), RayParam)

to

workspace:Raycast(HRP.Position, Vector3.new(0,-50,0), RayParam)

Also it depends what you’re using this on, but this may not be the most accurate. If you need the object the player is standing on for anticheat purposes, you will run into false positives eventually with this code.

Also, -50 should be about -5 for performance. I think it can be even lower (like -3.5) but I don’t remember.

1 Like