Casting a ray 100 studs towards touch event?

I’m currently trying to raycast 100 studs towards the position of the UserInputService.TouchStarted event to check for a target. I’m doing this by creating a unit ray casted towards the screen position and using it to create a new ray 100 studs long. The raycasting seems to work fine, however the range on it is ridiculously limited to maybe 1-2 studs. I’m guessing I’m doing something with extending the ray wrong but can’t for the life of me figure it out. The code is as follows:

UIS.TouchStarted:Connect(function(input, ProcessedByGame)
	if ProcessedByGame then
		return
	end

	local camera = game.Workspace.CurrentCamera

	local unitRay = camera:ViewportPointToRay(input.Position.X, input.Position.Y, 1)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * 100)
	local hitPart, worldPosition = game.Workspace:FindPartOnRayWithWhitelist(ray, CollectionService:GetTagged("NPCClickBox"))

	if hitPart then
		print("Found")
		ResetTarget()
		HighlightedNPC = hitPart.Parent

		if HighlightedNPC ~= nil then
			if ClickStartTime == nil then
				ClickStartTime = GlobalTime
			end
		end
	end	
end)

And a quick video demonstrating the limited range:

Hopefully just something simple I’m overlooking

Edit: Yep, figures. Shoud’ve been using ScreenPointToRay rather than ViewportPointToRay, the simple coincidence of the distances the angles lined up at had me convinced it was just using the unit ray length of 1.

2 Likes

Why don’t you just use Mouse.Target and check the distance between you and the NPC?
Oh I’m stupid, forgot you are making mobile interface, don’t mind this.

That’s not really a possibility here. The none touch input based version of the code uses raycasting as well with an RBXMouse module, but mouse events in general aren’t reliable enough on mobile for my tastes.

Have you tried visualizing the ray?

Also you could try workspace:Raycast(origin, direction, raycastParams)
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast

This might not be a fix, but try to use the new raycasting method. The reason for this is the old ray.new is deprecated

I’m aware but that has nothing to do with this issue, deprecation doesn’t mean it doesn’t work. I’m simply trying to understand why the unit rays direction * 100 is not resulting in a 100 stud ray, not trying to redo the approach when that’s the only thing wrong.

Try visualizing the ray to 100% make sure the issue is with the length of the ray. Because I don’t see the reason that the ray would actually be limited to smaller length (1-2 studs) when you are setting the direction to unitRay.Direction * 100.

And how do you suggest I do that without relying on the results of the raycast itself when the entire point of casting is to find the hit position? I have a utility module to visualize debug elements like that however it requires the start and end positions of the ray. I can feed it the camera position and 100 studs in the direction of the touch event but that’s simply feeding arbitrary numbers (the 100 studs or even unit * 100 as that’s what Im doing here but the results are clearly different) and not the actual results of the raycast.

May i just ask, how did you test the raycasting? (Thinking about camera and character position)

Just as I did in the video above essentially. I have it printing “Found” when a hit is found from the raycast. Clicking them from further out than they successfully attack isnt printing anything which is what leads me to think its the length of the cast, but I could be wrong. It’s using FindPartOnRayWithWhitelist however giving a whitelist of all objects with the tag “NPCClickBox”.

Oh right okay, you need to remember that you’re raycasting from the position of the camera. Not the position of your player

I mean, yeah I know but does that look like the cameras more than 100 studs away to you (assuming that’s what you meant). I think I found the actual issue though and @Sougood was correct. I didn’t have a good way to visualize the ray without the info it provides, however if I could’ve the problem wouldve been pretty evident. Using the old ViewportPointToRay instead of ScreenPointToRay, genuinely can’t believe I missed it this long.

Wow, I was just about to post that you should use :ScreenPointToRay(). Glad you found the fix though!

1 Like

Thank you for the help! My bad if I came off as rude or anything (reading back it kinda seems that way lol), just pressed for time.

1 Like