Raycasting Visualizer Doesn't Work In The Sky? How To Make This Work?

So I made a visualizer script for a module, creates a ray. There’s 2 huge problems that I want to fix, before making this script an actual projectile firing script.

  1. The aim isn’t accurate at all
    The part isn’t where my mouse is pointing, the part is just in the direction of it.
  2. In the sky the part doesn’t display at all
    This one’s pretty self explanatory.

Module:

local module = {}

function module:GrabPoints(Position, Root)
	local ray = Ray.new(Root.Position, Position.p)
	local part = Instance.new("Part");
	part.Material = Enum.Material.Neon;
	part.Size = Vector3.new(.2, .2, ray.Direction.magnitude);
	part.CFrame = CFrame.new(ray.Origin + ray.Direction/2, ray.Origin + ray.Direction);
	part.Anchored = true;
	part.CanCollide = false;
	part.Parent = game.Workspace.CurrentCamera;
end

return module

Also, Position = Mouse.Hit.p

Example of problem:

1 Like

local pointA = workspace.PartA.Position
local pointB = workspace.PartB.Position
local length = (pointA - pointB).Magnitude

local ray = Ray.new(pointA, pointB)

local part = Instance.new("Part");
part.Material = Enum.Material.Neon;
part.Size = Vector3.new(.2, .2, length);
part.CFrame = CFrame.new(ray.Origin, ray.Direction) * CFrame.new(0, 0, -length/2);
part.Anchored = true;
part.CanCollide = false;
part.Parent = workspace

I made some modifications to your code. I don’t think using the ray’s direction magnitude is the right way to get the length. Also I constructed the CFrame using the ray’s origin and direction. The final thing is to push the part forward by half of it’s length.

This did solve the problem where the lines are inaccurate, what about aiming in the sky though? Because projectiles are supposed to shoot anywhere you aim not just when you hit a part.

Edited Code:

local module = {}

function module:GrabPoints(Position, Root)
	local ray = Ray.new(Root.Position, Position.p)
	local part = Instance.new("Part");
	part.Material = Enum.Material.Neon;
	
	local length = (Root.Position - Position.p).Magnitude
	part.Size = Vector3.new(.2, .2, length);
	part.CFrame = CFrame.new(ray.Origin, ray.Direction) * CFrame.new(0, 0, -length/2);
	part.Anchored = true;
	part.CanCollide = false;
	part.Parent = game.Workspace.CurrentCamera;
end

return module

If you click in the sky, the magnitude of the two points will be about 10000. The part’s Z axis size seems to be limited to 2048. So a quick fix to this is to use -part.Size.Z/2 over -length/2.

Although ideally you would just limit the length. You can use math.clamp:
length = math.clamp(length, 0, 200)
that would make the maximum length equal to 200.

So like this?

local module = {}

function module:GrabPoints(Position, Root)
	local ray = Ray.new(Root.Position, Position.p)
	local part = Instance.new("Part");
	part.Material = Enum.Material.Neon;
	
	local length = (Root.Position - Position.p).Magnitude
	part.Size = Vector3.new(.2, .2, length);
	part.Size = part.Size - Vector3.new(0,0,-part.Size.Z/2)
	part.CFrame = CFrame.new(ray.Origin, ray.Direction) * CFrame.new(0, 0, -length/2);
	part.Anchored = true;
	part.CanCollide = false;
	part.Parent = game.Workspace.CurrentCamera;
end

return module
	part.CFrame = CFrame.new(ray.Origin, ray.Direction) * CFrame.new(0, 0, -part.Size.Z/2);

For the part’s CFrame

Yeah that works, only problem is when im aiming projectiles I won’t really have a part so what will I do then? -Position.Z/2?

yeah the problem isn’t that it doesn’t work in the sky, it’s just very far.

Why don’t you just limit the ray direction, by using local ray = Ray.new(Root.Position, Position.Unit * 10)

I’ll give that a try, and get back to you if it works.