How do I make the camera look to the mouse?

I’m currently making my own freecam and like the title says I can’t get the camera to look at the mouse correctly

Freecam code:

FreeCamConnection = RunService.RenderStepped:Connect(function(dt)
		local LookVector = Camera.CFrame.LookVector
		local RightVector = Camera.CFrame.RightVector
		local LeftVector = -RightVector
		local BackVector = -LookVector
		
		local IsADown = Keyboard:IsKeyDown(Enum.KeyCode.A)
		local IsDDown = Keyboard:IsKeyDown(Enum.KeyCode.D)
		local IsSDown = Keyboard:IsKeyDown(Enum.KeyCode.S)
		local IsWDown = Keyboard:IsKeyDown(Enum.KeyCode.W)
		
		local IsRightDown = Mouse:IsRightDown()
		
		Camera.CameraType = Enum.CameraType.Scriptable
		
		if IsWDown then
			Camera.CFrame += LookVector * 50.2 * dt
		end
		
		if IsSDown then
			Camera.CFrame += BackVector * 50.2 * dt
		end
		
		if IsADown then
			Camera.CFrame += LeftVector * 50.2 * dt
		end
		
		if IsDDown then
			Camera.CFrame += RightVector * 50.2 * dt
		end
		
		local MousePos = Mouse:GetPosition()
		
		local MouseRay = Camera:ScreenPointToRay(MousePos.X, MousePos.Y)
		
		if IsRightDown then
			--here would be the code to rotate the camera
		end
	end)

Just to understand how it would work

I tried:

Camera.CFrame = CFrame.new(Camera.CFrame.Position, Camera.CFrame.Position + MouseRay.Direction)

Thanks!

1 Like
1 Like