Need help with raycast

So I’ve been trying to make a sort of series of parts in front of the player that sequentially get bigger and further, and I’ve basically got it, but there is this weird interaction where if I raise the starting point of the raycast, the actual distance that it travels from the player changes.

Here’s the code:

local cf = char.HumanoidRootPart.CFrame




		for i = 0 , 300, 10 do
			task.wait()
			local dir = ((cf* CFrame.Angles(0,0.3,0)).LookVector * i + Vector3.new(0,-20,0)) 
			local params = RaycastParams.new()
			params.FilterType = Enum.RaycastFilterType.Include
			params.FilterDescendantsInstances = {workspace.Map}
			local craterRaycastResult

			local craterRaycastResult = workspace:Raycast(cf.Position, dir, params)
			local part = Instance.new("Part")
			part.Anchored = true
			part.CanCollide = false
			part.Parent = work
			part.Position = craterRaycastResult.Position

		end

There a reason you can’t just cast rays that go straight downward in front of the player here? This way if you jump it won’t go any further than not jumping:

local root: BasePart = script.Parent:WaitForChild('HumanoidRootPart')

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {workspace:WaitForChild('Baseplate')}

local NUMBER_OF_PARTS = 30
local STUD_INCREMENT = 2

game:GetService('UserInputService').InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed or input.KeyCode ~= Enum.KeyCode.F then
		return
	end
	
	local cf = root.CFrame
	
	local rootPosition = cf.Position
	local look = cf.LookVector
	
	for i = 1, NUMBER_OF_PARTS * STUD_INCREMENT, STUD_INCREMENT do
		local origin = rootPosition + look * i -- could probably exponentiate here if you want parts to go more than the increment (ie i ^ 1.25 or something)
		
		local direction = Vector3.new(0, -10000, 0)
		
		local ray = workspace:Raycast(origin, direction, params)
		
		if not ray then
			continue
		end
		
		local part = Instance.new('Part')
		part.Anchored = true
		part.CanCollide = false
		part.Position = ray.Position
		part.Parent = workspace
		
		task.wait()
	end
end)

Ah okay. It seems like my problem was that I was changing the direction of the raycast rather than the origin. Your code works perfectly.

1 Like

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