WorldModel Raycasting

Hello, I really don’t understand, how to raycast to mouse position via WorldModel in viewportFrame.
So, how to get mouse position in a Viewport for direction of ray?

Probably you’re looking for Mouse.UnitRay which will cast a ray, having origin as the Camera position and a direction with magnitude of 1 to where the mouse is positioned in the world, similar to (v1-v2).Unit.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local Client = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
local Mouse = Client:GetMouse()
	
RunService.RenderStepped:Connect(function()
	local Result = workspace:Raycast(Mouse.UnitRay.Origin, Mouse.UnitRay.Direction*100)
	
	if Result then
		print(Result.Instance)
	end
end)

does this work in ViewportFrame with WorldModel?

Since WorldModel inherits WorldRoot functions, you can use the :Raycast() function on the WorldModel in the ViewportFrame. Preferably from the camera.

example

local result = WorldModel:Raycast(origin, direction, raycastParameters)
print (result and result.Position)

does anyone have an answer been stuck at this and nothing in the internet

1 Like