How can I make all rays the same length when raycasting?

I’m pretty new to raycasting, and I wondered if there was any way to make all rays the same length when raycasting.

I’m making a collision system for a part with raycasting, and some of the rays are longer than others. The problem is that some rays would detect collisions earlier than others.

I have looked around the devforum and google for solutions, and haven’t figured out anything.
In my code is a ray visualizer so I could see the rays. I made it using this video. https://www.youtube.com/watch?v=m8nse0q1l5w

This is what it looks like at the moment.
111
The parts are there because of the ray visualizer.

local radius = 1
local function MakeRayVisible(ray, origin, direction)
	local midpoint = origin + direction/2

	local part = Instance.new("Part")
	part.Parent = workspace

	part.Anchored = true
	part.CFrame = CFrame.new(midpoint, origin)
	part.Size = Vector3.new(radius, radius, direction.magnitude)

	part.Material = Enum.Material.Neon
	part.BrickColor = BrickColor.new("Really red")

	return part
end

local directions = {
	Vector3.new(0,0,10),
	Vector3.new(5,0,10),
	Vector3.new(10,0,10)
}

for i = 1, #directions do
	local part = script.Parent
	local origin = part.Position
	local direction = directions[i]

	local ray = workspace:Raycast(origin, direction)

	MakeRayVisible(ray, origin, direction)
end

That is the only script I have for this system at the moment.

Is your Ray 1 stud long, or your visualization Part?
You have direction.magnitude, but since direction is a Vector3 it doesn’t have a Magnitude.
part.Size = Vector3.new(radius, radius, 1) would make your red Part 1 stud long.

This script was originally for the old ray casting method, but I updated it to the new method. I’m pretty new to raycasting, but I think the length is the Z value of direction. If I’m right would I just put direction.Z?

Hey there. You can do so by, literally, making them the same length. You’d need to know some vector math to do this.

local radius = 1
local function MakeRayVisible(ray, origin, direction)
	local midpoint = origin + direction/2

	local part = Instance.new("Part")
	part.Parent = workspace

	part.Anchored = true
	part.CFrame = CFrame.new(midpoint, origin)
	part.Size = Vector3.new(radius, radius, direction.magnitude)

	part.Material = Enum.Material.Neon
	part.BrickColor = BrickColor.new("Really red")

	return part
end

local directions = {
	Vector3.new(0,0,10), --This has length 10
	Vector3.new(5,0,10), -- This has length √125
	Vector3.new(10,0,10) -- This has length 100√2
}

local function CastToSize(vector, magnitude)
    return vector.Unit * magnitude -- Unit vector * Magnitude. High school vector math.
end


for i = 1, #directions do
	local part = script.Parent
	local origin = part.Position
	local direction = CastToSize(directions[i], 10) -- Now they are casted to the same size.

	local ray = workspace:Raycast(origin, direction)

	MakeRayVisible(ray, origin, direction)
end

You can read up about vector math at a highschool level. It’ll be helpful.

Please note that vectors, indeed have magnitude, by definition

1 Like

Ah, that’s a good thing to know!

1 Like

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