Better targeting using raycasting

I’m trying to improve my web swinging system. I have a targeting system using :Dot() to find out what part is the closest part to the player for the web to attach to. Here’s a little run down of how it works:

The script uses run service and uses a for i, v loop to loop through all the attachment points in the map. If it’s in the :Dot() range, it adds that part to the table and the part’s index in the table depends on how far it was from the player. The closer the part, the higher the index. Then it picks the first item in that table.

The system is good, but not great. It detects parts through walls and through other buildings, and also I don’t want it to detect parts under the player’s character, only above them. I was thinking of using raycasting to fix this. I know how to use raycasting, but I don’t know how I can do this efficiently. Can anyone please help me on how I can achieve this?

1 Like

IF your attachments are outside of the buildings and not inside of them, when you are initially creating the index of attachments inside of the :Dot() range, you could raycast to each one with some code like this:

local Viable = false

local RayOr = Character.HumanoidRootPart.Position
local RayDir = CFrame.new(RayOr, QuestionableAttachment.WorldPosition).LookVector * DotRange
local RayPar = RaycastParams.new()
RayPar.FilterType = Enum.RaycastFilterType.Blacklist
RayPar.FilterDescendantsInstances = {Character, anyothereffectfoldersetc}

local RayCast = workspace:Raycast(RayOr,RayDir,RayPar)

if RayCast and RayDir.Y > 0 then
	Viable = true
end

-- allow the attachment if Viable == true
1 Like