How to make a part face the direction of the camera

I’m trying to make a flying broom and currently, it does move however the broom doesn’t follow the camera’s direction. The broom has a handle that has a ConstraintWeld to the HumanoidRootPart. This is my code so far. I’ve tried a lot of ways but none of them seem to work.

How the broom works:

How the flying works without setting CFrame of the broom handle:

How the flying works with setting CFrame of the broom handle:

connection = flyingEvent.OnServerEvent:Connect(function(player, keyPressed)
	local character = player.Character or player.CharacterAdded
	--local humanoid = character:WaitForChild('Humanoid')
	--local currentCamera = game:GetService('Workspace').Camera
	local currentCamera = game:GetService('Workspace').CurrentCamera
	local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
	local cameraDirection = humanoidRootPart.CFrame:ToObjectSpace(currentCamera.CFrame).LookVector
	
	--currentCamera.CameraType = 'Scriptable'
	
	--broom.Handle.CFrame = CFrame.Angles(math.rad(currentCamera.CFrame.LookVector.X), math.rad(currentCamera.CFrame.LookVector.Y), math.rad(currentCamera.CFrame.LookVector.Z))
	--broom.Handle.CFrame = CFrame.new(broom.Handle.Position, Vector3.new(currentCamera.CFrame.LookVector.X * 900000, broom.Handle.Position.Y, currentCamera.CFrame.LookVector.Z * 900000))
	--broom.Handle.CFrame = CFrame.new(broom.Handle.Position, currentCamera.CFrame.LookVector)
	--humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position, currentCamera.CFrame.Position)
	--broom:WaitForChild('Handle').CFrame = humanoidRootPart.CFrame + Vector3.new(0, 5, 0)
	--broom:WaitForChild('Handle').CFrame = currentCamera.CFrame.LookVector + CFrame.new(0, -5, 0)
	--humanoidRootPart.CFrame = currentCamera.CFrame * CFrame.new(0, -5, 0)
	--broom.Handle.CFrame = CFrame.new(broom.Handle.Position, Vector3.new(broom.Handle.Position.X, currentCamera.CFrame.LookVector.Y, broom.Handle.Position.Z))
	--broom:WaitForChild('Handle').CFrame = CFrame.new(broom:WaitForChild('Handle').CFrame.Position) * currentCamera.CFrame.Rotation
	
	if keyPressed == Enum.KeyCode.W then
		broom.Handle.CFrame += Vector3.new(0, 0, -1)
		--broom.Handle.CFrame *= CFrame.new(0, 1, 0)
		--character:PivotTo(character:GetPivot() * CFrame.new(0, 0, -1))
	elseif keyPressed == Enum.KeyCode.A then
		broom.Handle.CFrame += Vector3.new(-1, 0, 0)
		--broom.Handle.CFrame *= CFrame.new(-1, 0, 0)
		--character:PivotTo(character:GetPivot() * CFrame.new(-1, 0, 0))
	elseif keyPressed == Enum.KeyCode.S then
		broom.Handle.CFrame += Vector3.new(0, 0, 1)
		--broom.Handle.CFrame *= CFrame.new(0, -1, 0)
		--character:PivotTo(character:GetPivot() * CFrame.new(0, 0, 1))
	elseif keyPressed == Enum.KeyCode.D then
		broom.Handle.CFrame += Vector3.new(1, 0, 0)
		--broom.Handle.CFrame *= CFrame.new(1, 0, 0)
		--character:PivotTo(character:GetPivot() * CFrame.new(1, 0, 0))
	elseif keyPressed == Enum.KeyCode.Space then
		broom.Handle.CFrame += Vector3.new(0, 1, 0)
		--broom.Handle.CFrame *= CFrame.new(0, 0, 1)
		--character:PivotTo(character:GetPivot() * CFrame.new(0, 1, 0))
	elseif keyPressed == Enum.KeyCode.LeftShift then
		--broom.Handle.CFrame += Vector3.new(0, 0, 0)
		--broom.Handle.CFrame *= CFrame.new(0, 0, 0)
		-- a
	elseif keyPressed == Enum.KeyCode.LeftControl then
		broom.Handle.CFrame += Vector3.new(0, -1, 0)
		--broom.Handle.CFrame *= CFrame.new(0, 0, -1)
		--character:PivotTo(character:GetPivot() * CFrame.new(0, -1, 0))
	end
end)

To copy the cameras rotation, do this

broom.Handle.CFrame = CFrame.new(broom.Handle.Position) * currentCamera.CFrame.Rotation

Also, be sure to add the camera’s relative vectors to the CFrame instead of world axes. For example, instead of:

broom.Handle.CFrame += Vector3.new(0, 1, 0)

Do:

broom.Handle.CFrame += currentCamera.CFrame.UpVector
1 Like