Getting No Raycast Result

Hello developers, I am currently working on a slot purchase system. I have created a system, when a slot gets touched, 8 raycasts fire near it to get the nearby slots. Consequently, we can work with a table of slots near the target slot to unlock new slots.
Unfortuately, the system doesn’t see any nearby slots, even tought there are. Here is my script and the slot systems image in order for you to have a clearer image of the situation.

local module = {}

local raycastLength = 12


-- PRINT THE RESULT
local function printRaycastRes(raycastResult)
	if raycastResult then
		print("Instance:" .. raycastResult.Instance)
		print("Distance:" .. raycastResult.Distance)
	else
		print("no res")
	end
end

-- FIRE THE RAY
local function fireRay(rayOrigin, rayDirection)
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	printRaycastRes(raycastResult)
end


-- CREATING THE PARAMETERS FOR RAYCAST (MAIN ISSUE)
function module.PurchaseSlot(slot)
	for i = 1, 8 do
		
		-- create a ray origin
		-- use if statements to create the ray direction parameters
		-- combine the origin with direction and fire 8 raycasts (this version has only 1 for testing)
		
		
		local rayOrigin = slot.Position + Vector3.new(0, 8, 0)
		local rayDirection
		
-- Assigning the positions (X and Z primarily to get all the slots nearby, there will be 8 if & elseif states)
		if i == 1 then
			rayDirection = slot.Position + Vector3.new(0, 0, raycastLength)
			
			fireRay(rayOrigin, rayDirection)
		end
	end
end

return module

print out items like your slot.Position so you can see where the actual items are.
Does the fact that your rayOrigin is 8 studs up from your slot.Position keep the ray from hitting anything?
You can try making a Part as a ray indicator that shows where it’s actually firing from.
Does the part the ray is supposed to contact have its CanQuery Property off?

1 Like

I have analysed the ray origin and the ray directions position carefully. Here they are:

Rays

The ray directly passes through the target slot, however, I get no raycast result. Also, the target slot has its CanQuery property on.

Also, as you can see there is a 3D model in between two positions. The models CanQuery property is off and I have also tried to completely delete it. Nothing seems to work.

check if canQuery is enabled on parts

Mehh, try this

rayDirection = (rayOrigin - slot.Position).Unit * raycastLength

and if it dont work try to switch the rayOrigin and slot.Position (slot.Position - rayOrigin) can’t remember which way around it is right now
edit: also try -raycastlength, trial and error is my favourite with raycasts to be honest

This actually helped me get a raycast result, however the result is only the part itself. Here is the actual problem, the ray doesn’t get a direction on the X or Z axis and only on Y. The issues image and the current script for you to get a better understanding is below.

rayDirection = slot.Position + (slot.Position - rayOrigin).Unit * Vector3.new(0,0,raycastLength)

Sorry, what I meant was place a Part that shows the actual location and orientation of the Part, not just where it’s origin is. These will show where the ray actually is, and then you can troubleshoot from there:
How do you visualize a raycast as a part? which links to How do I visualize a ray? - #4 by sjr04

1 Like

Yeah, I now managed to successfully raycast. However, here is an other issue. Now, the raycast hits the character if its in the way and doesn’t continue to raycast until it touches the slot. Basically, it returns the characters part instead continuing its path and touching the targeted slot.
I tried turning characters parts CanQuery to false when it gets added to workspace, however it also doesn’t seem to work. Is there an other solution for this?

In the Raycasting documentation check out the Filtering section. You can choose which Parts are able to be detected by the ray. It’ll ignore all other items.

1 Like

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