How to get what camera is looking on without mouse?

Is there any way to get a object that player is looking on without using mouse?
I need it for mobile support

You can ray cast from the camera using its look vector
Example:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAppearanceLoaded:Wait()

local Camera = workspace.CurrentCamera

local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Character}

local function GetLookatInstance(Distance:number)
	local Cast = workspace:Raycast(Camera.CFrame.Position, Camera.CFrame.LookVector*Distance, Params)

	return Cast and Cast.Instance or nil
end

while task.wait() do
	print(GetLookatInstance(25))
end

The code above filters out the player character and will continuously print whatever the player is looking at

1 Like

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