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.
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.
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
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.