Need help with raycasting

Hi, I’m practicing raycasting and created this thing where the player would press a button and a line of explosions would occur.

The problem is that the raycasting seems to be inaccurate.
When I’m on the ground the raycast has a shorter distance but when I jump it has a much farther distance.

Shown here:

What I made it do is create a ray some studs away and then shoot down to create the kaboom.

Here is the codeee:

local userinputService = game:GetService("UserInputService")
local debrisServce = game:GetService("Debris")

local camera = workspace.CurrentCamera

userinputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		local player = script.Parent
		local humanoidRootPart = player.HumanoidRootPart
		local studs = 100
		local repeatCount = 0
		
		local rayparams = RaycastParams.new()
		rayparams.FilterDescendantsInstances = {player}
		
		repeat
			local cameraLookVector = humanoidRootPart.CFrame.LookVector * studs
			local ray = workspace:Raycast(humanoidRootPart.Position, cameraLookVector - Vector3.new(0, 30, 0), rayparams)
			
			if ray then
				local object = ray.Instance
				
				local testPart = Instance.new("Part")
				testPart.Anchored = true
				testPart.CanCollide = false
				testPart.CanTouch = false
				testPart.CanQuery = false
				testPart.Orientation = humanoidRootPart.Orientation
				testPart.Position = ray.Position
				testPart.Parent = workspace
				
				local explode = Instance.new("Explosion")
				explode.Position = testPart.Position
				explode.Parent = testPart
				
				debrisServce:AddItem(testPart, 0.5)
			end
			
			task.wait(0.1)
			studs += 100
			repeatCount += 1
		until repeatCount == 20
		
	end
end)

The studs are 100 because if it was smaller the explosion would be created under the player and unalive them.
Also, I would like to know how to make the parts not constantly follow the player’s direction and only lock into a straight line.
I’ve tried moving the cameraLookVector out of the loop but it wouldn’t follow the direction anymore. Making it only look in one direction every time.

1 Like

for moving in a straight line simply move

local cameraLookVector = humanoidRootPart.CFrame.LookVector * studs

out side of the loop