Why do these rays rarely return a result?

local function multiRay(origins)
	local finalResult
	local finalDistance
	
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = ignoreList
	params.IgnoreWater = true

	for i, v in pairs(origins) do
		local result = workspace:Raycast(v.WorldPosition, v.CFrame.LookVector * 4.5, params)

		if result then
			local distance = (v.WorldPosition - result.Position).Magnitude

			if finalResult then
				if (distance < finalDistance) then
					finalResult = result
					finalDistance = distance
				end
			else
				finalResult = result
				finalDistance = distance
			end
		end
	end

	return finalResult
end

local function onActivate(hand)
	hitVictims = {}
	local result

	if hand == "Left" then
		result = multiRay(leftOrigins)
	elseif hand == "Right" then
		result = multiRay(rightOrigins)
	end

	if result then
		local hit = result.Instance

		if hit then
			local human = hit.Parent:FindFirstChild("Humanoid")

			if human and human.Health > 0 then
				
				if table.find(hitVictims, human) then 
					print("Humanoid already hit: "..hit.Parent.Name)
					return 
				else
					print("Humanoid not already hit: "..hit.Parent.Name)
				end

				-- tagHuman(human)
				human:TakeDamage(10)
				table.insert(hitVictims, human)
			end
		end
	end
end

Basically, there are four attachments places on each of the character’s arms, and depending on which animation plays, it sends a “HandInUse” attribute to check which arm is being swung and casts rays out of that arm’s attachments. The multiRay() function is used to shoot multiple rays and check which one hit something the closest.

For whatever reason, it rarely works. I’m not really sure why.

4bd2f640b4a4a0aa9df29fa45f034aaa

(The attachments are facing the right direction, although I don’t know if the rays are being shot in the right direction, nor do I know if the rays are being shot far enough.)

If you have any ideas on how to make this work, please tell. I can always provide more of the code incase what I provided isn’t enough to work with.

EDIT:

Got the visualizers to work(?), and I figured out how to set the attachments to face the right way. Now, the rays are only registering when I’m facing towards the south. I have no idea on why this could be happening or how to fix it. Any ideas?

86f0569935f75846269d6c03fcc2fb02

Have you tried visualizing the rays? I suggest you look into this post

It’s helped me quite a lot in my FPS game.

1 Like

Oh, yea I actually have. Though, it didn’t do much for me since I need the RaycastResult to calculate the distance of the ray in order to set the Size and CFrame of the visualizer.

local result = workspace:Raycast(origin, direction, params)

if result then
    local distance = (origin - result.Position).Magnitude -- what I am referring to

    local p = Instance.new("Part")
    p.Anchored = true
    p.CanCollide = false
    p.Size = Vector3.new(0.1, 0.1, distance) -- distance var used here..
    p.CFrame = CFrame.lookAt(origin, position)*CFrame.new(0, 0, -distance/2) -- ..and here
end

Would you know a way to get around that?

Just do

local distance = if result then (origin - result.Position).Magnitude else origin + direction

And get rid of the if result if statement

2 Likes

Hm, I don’t believe I really understand. I tried that out and this was the outcome; all of the visualizers seem to be facing the same direction and are really small:

c47b575b958a9e25071b4cf30a429ed6

Maybe it’s colliding with something? What’s the ignoreList variable set to?

No, I believe it’s a problem with the calculations.

There’s really no way they can be that small unless the size is negative, or unless they hit something very close.

Here’s the updated code, haven’t touched it since yesterday:

Also, the ignoreList variable is a table containing the tool and character, as well as noncollide and/or transparent parts.

You’re missing a .Magnitude in the first distance variable. Change

local distance = if result then (v.WorldPosition - result.Position).Magnitude else v.WorldPosition + (v.CFrame.LookVector * 4.5)

to

local distance = if result then (v.WorldPosition - result.Position).Magnitude else (v.WorldPosition + (v.CFrame.LookVector * 4.5)).Magnitude
1 Like

Ah, don’t know why I didn’t think of that. The parts are no longer that small anymore.

Something else is happening now, though. They all point and extend towards the center point of the map:

f63bd8c2758133832d612b587eb8ce08

v.Position + v.CFrame.LookVector * 4.5

I guess, it’s getting closer. They still point towards the center of the map, though:

aabbd8f28ef79ab3f89239c45082419a

A CFrame’s LookVector returns solely the vector such as Vector3.new(0,0,-1) and doesn’t automatically sum up with the position, you have to additionally add its position too. This is why you should sum it up with the part’s position so it is offsetted from the part’s position, not the world’s center.

You should change everything associating with “LookVector” to also sum up with its position. Everything. Make sure you have correctly set every single one of them.

Apologies for bumping this but I just can’t figure this out and could really use some help. I have a limited knowledge of raycasting, and I can’t seem to find any other posts similar to my problem here.

@hallowynl, could you please explain how I would “sum it up with the part’s position, not the world’s center”? I don’t seem to understand what you meant by that.