Raycast (and shapecast) not hitting anything even if something is there

I don’t know why on God’s green earth this is happening, and I’ve just given up trying to find the issue, and yes, it’s doing exactly what the title of this post says.

Basically, I was trying to make a function that can check to see if the humanoid can simply walk straight to the player by first raycasting from the figure to the player to see if there’s nothing in the way and then (this is the part that doesn’t work) raycasting along the “line” while being directed down to see if there’s ground there. :confused:

I’ve even replaced the raycast with spherecast, and it did the same thing!

The function:

local function Straight_line_check(start_pos, target_pos, target_model)
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {figure, target_model}
	params.FilterType = Enum.RaycastFilterType.Exclude
	local direction = target_pos-start_pos
	local RAY = workspace:Spherecast(start_pos, 2, direction, params)
	local success = pcall(function()
		local A = RAY.Instance
	end)
	if success then
		return {}
	else	
		local step = 2/direction.Magnitude
		local current = step
		local Points = {}
		repeat
			local result_pos = start_pos + (direction*current)
			local cast = workspace:Spherecast(result_pos, 1, Vector3.new(0, -4, 0))
			local success, Error = pcall(function()
				local A = RAY.Instance
			end)
			if not success then
				return {}
				--	local wrong_pos = result_pos-Vector3.new(0, 4, 0)
				--	local part = Instance.new("Part")
				--	part.Parent = workspace
				--	part.Position = wrong_pos
				--	part.Anchored = true
				--	part.CanCollide = false
				--	part.CanTouch = false
				--	part.CanQuery = false
				--	part.Shape = Enum.PartType.Ball
				--	part.Material = Enum.Material.Neon
				--	part.Size = Vector3.new(3.5, 3.5, 3.5)
			end
			local point = PathWaypoint.new(result_pos, Enum.PathWaypointAction.Walk, "Unblocked")
			table.insert(Points, point)
			current += step
		until current >= 1
		return Points	
	end
end

Here’s a Desmos graph of how it would look like:

You have to multiply your direction variable by how many studs you want it to go… yeah (for both spherecasting and raycasting)

you can probably use the LookVector of the starting part to get the correct direction too

Pretty sure all you need to do is do
Direction = (startpos - endpos).magnitude

Even i forget to do this

Im trying to have raycasts between the two points also aim down so to detect if theres ground there or not.

1 Like

Wouldn’t that give me the length/distance of the direction?

My bad
Try doing
Direction = (startpos - endpos).unit * (startpos - endpos).magnitude
This should do it, as it multiplies the dirextion with diatance

1 Like

I mean… i tried this and it didn’t fix the main problem regarding the raycasts not hitting anything even if somethings there but thanks i guess?

Welp i found the solution witch was simply replacing the second raycast with the deprecated one :confused:

local function Straight_line_check(start_pos, target_pos, target_model)
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {figure, target_model}
	params.FilterType = Enum.RaycastFilterType.Exclude
	local direction = target_pos-start_pos
	local RAY = workspace:Spherecast(start_pos, 2, direction, params)
	local success = pcall(function()
		local A = RAY.Instance
	end)
	if success then
		return {}
	else	
		local step = 2/(direction.magnitude)
		local current = step
		local Points = {}
		repeat
			local result_pos = start_pos + (direction*current)
			local hit = workspace:FindPartOnRay(Ray.new(result_pos, Vector3.new(0, -4, 0)), target_model)
			if not hit then
				return {}
			end
			local point = PathWaypoint.new(result_pos, Enum.PathWaypointAction.Walk, "Unblocked")
			table.insert(Points, point)
			current += step
		until current >= 1
		return Points	
	end
end