Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Need help with ScreenPointToRay? I've been looking for awhile now.

Hello, I've been trying for a long time now but I've found nothing that helps, How do I use :ScreenPointToRay? I'm trying to make a very customizable gun, but it's not working, The furthest I've gotten was creating a part at the cameras origin, but nothing else

I've read

But this didnt help, any help is Greatly appreciated, Thank you!

2 comments

0

Script?

0

For guns you should be using :ViewportPointToRay

2 answers

✓ Accepted answer

ScreenPointToRay

I have never heard of this API before in my life. Try this:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = Player:GetMouse()
local camera = workspace.CurrentCamera 
local length = 500 
local unitRay 
local ray 

game:GetService(RunService).RenderStepped:Connect(function()
	unitRay = camera:ScreenPointToRay(mouse.X, mouse.Y)  --idk if this will work but try this. Mouse.X and Mouse.Y is the mouse position on the screen. Since it's constantly moving, you, want to update this using RenderStepped
	ray = Ray.new(unitRay.Origin, unitRay.Direction * length) -- x is where the ray starts, y is the direction times the length. You can change the origin to the camera if you want as well as the length
end)

these are the basics of what i know about rays




script.Parent.Activated:Connect(function()
	
	local camera = workspace.CurrentCamera
	local length = 500
	local unitRay = camera:ScreenPointToRay(100, 100)
	local player = game.Players.LocalPlayer
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)
	
	-- above is from what i read on the wiki
	
	
	local part = Instance.new("Part", workspace)
	part.Name = "NewPart"
	part.CFrame = unitRay --CFrame expected
	
	
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.