Creating a Visualized Raycast to every Character HumanoidRootPart

Im trying to create a Raycast that only you can see with a keybind that connects from your HumanoidRootPart to anyone elses HumanoidRootPart, and im wondering how would I do so?

Example

Basic Visualized Raycast Script

local function VisibleRay(ray)
	local MidPoint = ray.Origin + ray.Direction / 2
	local Part = Instance.new("Part", workspace)
	Part.Anchored = true
	Part.CanCollide = false
	Part.CFrame = CFrame.lookAt(MidPoint, ray.Origin)
	Part.Size = Vector3.new(Size, Size, ray.Direction.magnitude)
	return Part
end

local StartCFrame = game.Workspace.Start.CFrame -- the block that sends the raycast
local TargetCFrame = game.Workspace.Target.CFrame -- block that receives the raycast

local StartPosition = StartCFrame.Position --position you want the ray to start at
local Distance = (StartPosition - TargetCFrame.Position).magnitude --how far you want the ray to go (the distance between the two parts)
local ray = Ray.new(StartPosition, (TargetCFrame.Position - StartPosition).Unit * Distance) --creating the ray

VisibleRay(ray)

I did a little bit of testing in studio and I think this is what you’re looking for:

local function connectParts(part1: Part, part2: Part)
    local base = (part1.Position - part2.Position)
	local dist = base.Magnitude
	local dir = base.Unit
	
	local part = Instance.new("Part")
	part.Anchored = true
	part.Parent = workspace
	part.Size = Vector3.new(1, 1, dist)
	part.CFrame = CFrame.lookAt(
        part1.CFrame:Lerp(part2.CFrame, 0.5).Position, 
        part2.Position
    )
end

Part1 must be the origin part (a.k.a your characters HumanoidRootPart).

Keep in mind that this script does not account for line of sight, so you have to code that in yourself.

Thank you this is exactly what im looking for.

1 Like

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