Raycasting between two attachments

Hello, I need help with raycasting between two attachment points, as shown in the model below:
image

This is the current code block that handles raycasting:

local hitrayparams = RaycastParams.new()
hitrayparams.FilterDescendantsInstances = { tool.Handle, humanoidinstance }
hitrayparams.FilterType = Enum.RaycastFilterType.Blacklist
local hitray = game.Workspace:Raycast(tool.Handle.Hit.WorldPosition, tool.Handle.Hit2.WorldOrientation, hitrayparams)
		
if hitray then -- for testing
	hitray.Instance.Color = Color3.fromRGB(0, 0, 0)
	print(hitray.Instance)
end
2 Likes

Hello, when you are providing the second argument for a Raycast, you only provide the direction but not the range.

Try translating the WorldOrientation to a CFrame and grabbing the LookVector*100

1 Like
-- change it to

local hitray = game.Workspace:Raycast(tool.Handle.Hit.WorldPosition, tool.Handle.Hit2.WorldPosition - tool.Handle.Hit.WorldPosition, hitrayparams)

v1 - v2 equals a direction vector pointing from v2 to v1. That is what I changed.

1 Like

Alright I’ll try this, I’ll respond once I’ve tried it

1 Like

I’m unsure if this would be good for hit detection, for when I was testing it, it was extremely hard to hit players
Here’s the new code:

local stopray = false
tool.Handle.Trail.Enabled = true
task.wait(0.2)
local hitrayparams = RaycastParams.new()
hitrayparams.FilterDescendantsInstances = { tool.Handle, humanoidinstance }
hitrayparams.FilterType = Enum.RaycastFilterType.Blacklist
spawn(function()
	while stopray == false do
		task.wait()
		local hitray = game.Workspace:Raycast(tool.Handle.Hit.WorldPosition, tool.Handle.Hit2.WorldPosition - tool.Handle.Hit.WorldPosition, hitrayparams)
		local hitcheck = script.Part:Clone()
		hitcheck.Parent = game.Workspace
		hitcheck.Position = tool.Handle.Hit2.WorldPosition

		if hitray then
			if hitray.Instance.Name == "HumanoidRootPart" or hitray.Instance.Name == "Torso" or hitray.Instance.Name == "Head" or hitray.Instance.Name == "Left Leg" or hitray.Instance.Name == "Left Arm" or hitray.Instance.Name == "Right Leg" or hitray.Instance.Name == "Right Arm" then
				local humanoid = hitray.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
				humanoid.Health = humanoid.Health - 35
				break
			end
			print(hitray.Instance)
		end	
	end	
end)
task.wait(0.35)
stopray = true

Please excuse the sloppy code

Change the direction parameter of the raycast function to:

(tool.Handle.Hit2.WorldPosition - tool.Handle.Hit.WorldPosition).Unit * 25

This makes the ray extend farther

This works a bit better, would there be any other way to do hit detection with swings since, it’s extremely hard to land a successful hit and I’m unsure if it’s the raycast or the positioning

Try out this module

I don’t wish to sound stubborn, but I’d like not use modules, although, it seems like it creates multiple rays at a 0.5 seconds interval

Update: I’ve made my own working version of the module in under 62 lines, it works too well actually, thank you for helping me with raycasts

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