How to get the players lookvector for a drag script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m trying to add mobile support for my drag script. For PC players I use the mouse location for the part position and for mobile I want to use the players current camera look vector. When the player rotates their screen on a mobile device, I want the part to mimic the players look vector. I’ve been researching and trying different solutions for days and still can’t figure it out so now I’m here.

  1. What is the issue? Include screenshots / videos if possible!
part.CFrame = player.Character:FindFirstChild("Head").CFrame + (player.Character:FindFirstChild("Head").CFrame.LookVector * 6)

this is the line of code that I’m having issues with. It follows the characters head CFrame but it doesn’t account for the Y axis so when the player looks down the part doesn’t go down with it.

Here is the full drag code for context

function beginDrag (part)
	ismoving = true

	local Center = Instance.new("Attachment")
	Center.Parent = part
	Center.Name = "AttatchFixer"

	local AlignOrient = Instance.new("AlignOrientation")
	AlignOrient.Name = "OrientationFixer"
	AlignOrient.Mode = Enum.OrientationAlignmentMode.OneAttachment
	AlignOrient.Attachment0 = Center
	AlignOrient.RigidityEnabled = true
	AlignOrient.Parent = part

	part.CanCollide = true

	movepart = runservice.RenderStepped:Connect(function()
		local getmouse = uis:GetMouseLocation()
		local viewportcam = currentCam:ViewportPointToRay(getmouse.X, getmouse.Y)

		if uis.MouseEnabled then	
			part.Position = player.Character:FindFirstChild("Head").Position + (viewportcam.Direction * 8)
			AlignOrient.CFrame = player.Character:FindFirstChild("Head").CFrame

		elseif uis.TouchEnabled then
			part.CFrame = player.Character:FindFirstChild("Head").CFrame + (player.Character:FindFirstChild("Head").CFrame.LookVector * 6)
			AlignOrient.CFrame = player.Character:FindFirstChild("Head").CFrame
		end
	end)
end

From my understanding of your issue, you’re trying to obtain the angle of the player’s screen that they’re looking through, the camera’s angle. In your code, you’re obtaining the CFrame of the Head of their character, but that’s not actually what the player uses to look around. Instead, in a local script, you can obtain the angle of the player’s Camera.

--local script
local cam = workspace.CurrentCamera
local function getCameraAngle()
	return cam.CFrame.LookVector
end

I’m not sure if this is what you’re asking for, but it does seem like it.

2 Likes

ty for the help but i tried the adjustments and i’m still having the same issues :frowning:

I

Could you show me the script you are using in that video clip?

function beginDrag (part)
	print(grabbedfood)
	if part.Name == "Knife" then
		UsingTool = true

		local Center = Instance.new("Attachment")
		Center.Parent = part
		Center.Name = "AttatchFixer"
		local AlignOrient = Instance.new("AlignOrientation")
		AlignOrient.Name = "OrientationFixer"
		AlignOrient.Mode = Enum.OrientationAlignmentMode.OneAttachment
		AlignOrient.Attachment0 = Center
		AlignOrient.RigidityEnabled = true
		AlignOrient.Parent = part



		part.CanCollide = true
		movepart = runservice.RenderStepped:Connect(function()

			local startpos = player.Character:FindFirstChild("Head").CFrame
			local getmouse = uis:GetMouseLocation()

			local viewportcam = currentCam:ViewportPointToRay(getmouse.X, getmouse.Y)

			--local rayresult = workspace:Raycast(viewportcam.Origin,viewportcam.Direction * 10, cfg)
			if uis.MouseEnabled then
				part.Position = player.Character:FindFirstChild("Head").Position + (viewportcam.Direction * 6)
				--part.Position = player.Character:FindFirstChild("Head").Position + (viewportcam.Direction * 8)
				--part.Position = viewportcam.Origin + viewportcam.Direction* (5)
				AlignOrient.CFrame = player.Character:FindFirstChild("Head").CFrame

			elseif uis.TouchEnabled then
				print("test")
				part.CFrame = currentCam.CFrame.LookVector
				--part.CFrame = player.Character:FindFirstChild("Head").CFrame + (player.Character:FindFirstChild("Head").CFrame.LookVector * 6)
				--part.Position = viewportcam.Origin + viewportcam.Direction* (5)
				AlignOrient.CFrame = player.Character:FindFirstChild("Head").CFrame


			end
		end)
	else
		ismoving = true

		local Center = Instance.new("Attachment")
		Center.Parent = part
		Center.Name = "AttatchFixer"
		local AlignOrient = Instance.new("AlignOrientation")
		AlignOrient.Name = "OrientationFixer"
		AlignOrient.Mode = Enum.OrientationAlignmentMode.OneAttachment
		AlignOrient.Attachment0 = Center
		AlignOrient.RigidityEnabled = true
		AlignOrient.Parent = part



		part.CanCollide = true
		movepart = runservice.RenderStepped:Connect(function()

			local startpos = player.Character:FindFirstChild("Head").CFrame
			local getmouse = uis:GetMouseLocation()

			local viewportcam = currentCam:ViewportPointToRay(getmouse.X, getmouse.Y)

			--local rayresult = workspace:Raycast(viewportcam.Origin,viewportcam.Direction * 10, cfg)
			if uis.MouseEnabled then
				part.Position = player.Character:FindFirstChild("Head").Position + (viewportcam.Direction * 8)
				--part.Position = viewportcam.Origin + viewportcam.Direction* (5)
				AlignOrient.CFrame = player.Character:FindFirstChild("Head").CFrame

			elseif uis.TouchEnabled then

				part.CFrame = player.Character:FindFirstChild("Head").CFrame + (player.Character:FindFirstChild("Head").CFrame.LookVector * 6)
				--part.Position = viewportcam.Origin + viewportcam.Direction* (5)
				AlignOrient.CFrame = player.Character:FindFirstChild("Head").CFrame


			end
		end)
	end
end

I think it’s because you only edited the code for the Knife tool, and not for the code under the else part of your if-statement.

oh man i always do this lol. let me adjust it now and see if it works!!

Looking at the code you wrote I don’t think it will work, as you’re setting a CFrame to a lookVector.
Perhaps try this instead:

local headp=player.Character:FindFirstChild("Head").Position
part.CFrame=CFrame.new(headp,headp+currentCam.CFrame.LookVector)*CFrame.new(0,0,-5)
2 Likes

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