Help understanding mouse raycasting

I took code from a DevForum post for a project of mine. The issue is I need to implement more features to this code and make some changes to it. I actually don’t know anything about how this code works.

The code:

local UIS = game:GetService("ReplicatedStorage") -- // I obviously understand how getting services works

UIS.InputBegan:Connect(function(input) -- // I have not used UIS much but I think I pretty much understand this function
	if input.UserInputType == Enum.UserInputType.MouseButton1 then -- // I understand this conditional statement
		
		-- // From here down is where I have no idea what's happening
		local cursorPosition = game:GetService("UserInputService"):GetMouseLocation()
		local oray = game.Workspace.CurrentCamera:ViewportPointToRay(cursorPosition.x, cursorPosition.y, 0)
		local ray = Ray.new(game.Workspace.CurrentCamera.CFrame.p, (oray.Direction * 1000))
		
	end
end)

The issue is I don’t know what’s going on in this script. For instance, I don’t know what :GetMouseLocation() is or does. Or what CurrentCamera:ViewportPointToRay means or what the parameters for the :ViewportPointToRay mean. I have no understanding of ray.new or it’s parameters.

I’m basically clueless and was hoping someone could explain in depth what all of these lines mean. I’ve searched DevForum and though I’ve come across a few posts that sort of explained things, none of them were detailed enough. I’d like to know how to use Mouse Raycasting on my own as a few other things in my game will require it. I know raycasting is a useful skill and would like to learn it. Any help much appreciated!

Go to the Documentation (create.roblox.com) and there should be short explanations (for references only) of each of those items.

Aren’t there tutorials about raycasting there too? I can’t remember.

1 Like

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