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

Raycast range not working properly?

local function createRay(tool,filter,mousePosition,range)
	if tool:FindFirstChild("Handle")then
		local rayOrigin = tool.Handle.Position
		local rayDirection = (mousePosition - rayOrigin).unit*range
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {filter}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

		if raycastResult then
			print("raycasted")
			local hitPart = raycastResult.Instance
			tempBullet(rayOrigin,raycastResult.Position)
		else
			
		end
	end
end

Many tutorials I've seen use

local rayDirection = (mousePosition - rayOrigin)*range--this is wrong because it makes the range way too high instead of 
local rayDirection = (mousePosition - rayOrigin).unit*range

When I use the one with .unit, it works only in the ranges that are not passed what I set the range to. lets say range = 50 if I click anywhere passed 50 studs the ray isn't even created

1 comment

0

I'm not sure if I am fully understanding your question, but couldn't you raise your raycast range to be higher? Lets say 1000000 instead of 50?

1 answer

✓ Accepted answer

If I remember correctly, Roblox said try not to use game.Players.LocalPlayer:GetMouse() as it has been superseded by game:GetService("UserInputService"):GetMouseLocation() This script may solve your problem, I have not tested this. Do keep in mind that this is a LocalScript You can read more about UserInputService here. If there's any error or you don't understand, do let me and i'll answer them asap!

local tool = script.Parent
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local function createRay()
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {tool.Parent}
	params.FilterType = Enum.RaycastFilterType.Blacklist
	
	local mousePosition = uis:GetMouseLocation()
	local unitRay = camera:ScreenPointToRay(mousePosition.X, mousePosition.Y)
	local raycastResult = workspace:Raycast(tool.Handle.Position, unitRay.Direction * 50, params)
	
	if raycastResult then
		-- whatever code goes here
	end
end)

5 comments

0

GetMouseLocation() seems to be your screen location for UIs, but using userinputservices does seem better for detecting the inputs since its also compatible for mobile. It doesn't really answer my question for the raycast to not work properly though

0

to get the postion using userinputservices seems to be input.Position? im not sure but ill test and see

0

Yes, GetMouseLocation gets the ui pixels. Then that's what the camera:ScreenPointToRay is for. It get the 3d position of the mouse in the world given the two arguments specifically the ui pixels.

0

Doesn't exactly answer my question about why the new RayCast method wont work but the old does for me, but none the less the knowledge you provided has helped me make a more secure system.

0

nvm i thought if i used that method to get the screen position locally and then let the server handle everything else it'd be more secure to prevent aimbots, but just realized they can use WorldToScreenPoint

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