Raycasting for head not working

I am trying to cast a ray from the players head into the direction of the players head but the ray seems to shoot directly down instead
This code is so the player can grab things in front of them similar to cleaning simulator

local player = workspace:WaitForChild(script.Parent.Parent.Parent.Parent.Name)
local PlayerHead = player:WaitForChild("Head")
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances  = {workspace["CanClick"]}
raycastParams.FilterType  = Enum.RaycastFilterType.Whitelist 
raycastParams.IgnoreWater  = true
script.Parent.InputBegan:Connect(function(_input)
	local rayOrigin  = PlayerHead.Position
	local rayDirection  = PlayerHead.Orientation
	local raycastResult  = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	if _input.UserInputType == Enum.UserInputType.MouseButton1 then
		if raycastResult  then
			local testpart = game:GetService("ReplicatedStorage")["ClickedPart"]:Clone()
			testpart.Parent = workspace
			testpart.Position = raycastResult.Position
			print(workspace:FindFirstChild(script.Parent.Parent.Parent.Parent.Name):FindFirstChild("Head").Orientation)
		end
	end
	if _input.UserInputType == Enum.UserInputType.MouseButton2 then
		if raycastResult  then
			local testpart = game:GetService("ReplicatedStorage")["ClickedPart"]:Clone()
			testpart.Parent = workspace
			testpart.Position = raycastResult.Position
			print(workspace:FindFirstChild(script.Parent.Parent.Parent.Parent.Name):FindFirstChild("Head").Orientation)
		end
	end
end)

(the player is using a custom player modal and the code is located inside of a gui button that covers the whole screen)

1 Like

So why not use CFrame.LookVector?, It gives you the direction the Object is facing, Note that when you are using this, you have to multiply it a certain amount, because when raycasting, you have to account for the range of it.

local range = 17 -- example
local rayDirection = PlayerHead.CFrame.LookVector * range -- Direction of Head times the range
local result = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

I tried it and it worked, thank you

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