Multiple Raycast

I’m improving my web swing for my game and it doesn’t attach to the buildings properly,
some of them are left floating in the air. How can I shoot multiple raycasts around me so I can detect the ideal position of the origin point

This is what I want to achieve, the circle is the player, the boxes are the buildings, and the lines are the rays.

1 Like

You could have a for-loop iterate from 0 to 2*pi, on some unit of radians as an interval, and use that to determine an angle from the players’ RootPart, hand, whatever the origin is, along the y-axis. From there you could raycast at whatever x/z angle you need to. Loop until you find a building to attach to, and if the loop finishes without the ray hitting anything, don’t shoot a web.

Looks like he wants a ray to be casted every 22.5 degrees.

math.rad(22.5) should suffice.

1 Like
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()

local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterDescendantsInstances = Character
RaycastParameters.FilterType = Enum.FilterType.Blacklist
RaycastParameters.IgnoreWater = true

local function CastRays()
	local Origin = HRP.Position
	local Target = Mouse.Hit.Position
	local Unit = (Target - Origin).Unit
	local Direction = Unit * 1000

	local RaycastResults = {}
	for Angle = 0, 360, 22.5 do
		local RaycastResult = workspace:Raycast(Origin, (CFrame.new(Direction) * CFrame.Angles(0, math.rad(Angle), 0)).Position, RaycastParameters)
		if RaycastResult then
			table.insert(RaycastResults, RaycastResult)
		end
	end
	return RaycastResults
end

local RaycastResults = Mouse.Button1Click:Connect(CastRays)

This creates an array of RaycastResult instances.

3 Likes