I'm trying to make a module function that makes visual representations of rays. The problem is, I'm not all that great at CFrame/Vector3 positioning. Here's what I have:
function module.viewray(ray) local part = Instance.new("Part", workspace) part.Name = "Ray" part.BrickColor = BrickColor.Red() part.Transparency = .7 part.TopSurface = Enum.SurfaceType.Smooth part.BottomSurface = Enum.SurfaceType.Smooth part.CanCollide = false part.Anchored = true part.CFrame = CFrame.new(ray.Direction/2, ray.Direction) --Here I'm just guessing what I have to do part.FormFactor = Enum.FormFactor.Custom part.Size = Vector3.new(.2, .2, ray.Direction.magnitude) --Here too end
And then I try to use that module function here:
viewray = require(game.ServerScriptService.Rays).viewray origin = Vector3.new(0, 50, 0) direction = Vector3.new(50, 50, 50) --Basically I'm trying to make a laser going across the sky ray = Ray.new(origin, direction) --Guessing here too viewray(ray)
And that creates a laser that doesn't go across the sky, but shoots from the center of the map to the sky. If someone could provide a brief walkthrough on rays, that would be great.
You're incredibly close. The only thing that I see missing is that module.viewray
never inspects the .Origin
property -- you're only ever looking at .Direction
.
The simplest solution is to add it to the CFrame:
part.CFrame = CFrame.new(ray.Direction/2, ray.Direction) + ray.Origin
Alternatively,
part.CFrame = CFrame.new(ray.Origin + ray.Direction/2, ray.Origin + ray.Direction)