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.
(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?
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
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:
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.