How to Raycast Down From Where The Player Is Looking?

So I’m trying to make a script that fires a Raycast down from 20 studs behind the player, then I want to set a part position or cframe to that Raycast.Position.

I can’t figure out how to set the RayOrigin in the Z value to always be 20 studs behind the location that the player is facing.

local function behindplayer()

	local randomplr = selectPlayer()
	local char = randomplr.Character
	local hrpcframe = char:FindFirstChild("HumanoidRootPart").CFrame
	local hrppos = hrpcframe.Position
	
	local rayOrigin = hrpcframe.Position + Vector3.new(0,100, hrpcframe * 10) -- Here is the issue!
	local rayDirection = Vector3.new(0,-200,0)

	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

	if raycastResult ~= nil then
		npc.PrimaryPart.CFrame = CFrame.new(raycastResult.Position)
		print("Setting location!")
	end
end

All Help is appreciated!

Are you looking to have it cast 10 studs in the -LookVector direction? Or just 10 studs in the positive Z direction relative to world coords?

If you want the former, you’d probably need to do something like:

local rayOrigin = rootPart.Position - root.LookVector * 10

I’m looking to have the origin 100 studs over the player’s rootpart, and the Z value 20 studs behind the player, and I’m trying to get the Z value to be in the direction the player is looking

Changed your code up a bit and managed to get it to work, thank you so much :slight_smile:

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