Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Completely confused with rays?

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.

1 answer

✓ Accepted answer

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)

2 comments

0

...Still, It's a going up diagonally. I'm trying to get a completely horizontal line going across the sky.

1

The second parameter to Ray.new is the direction (NOT the end-point). You told it to go up (the Y is 50). If you want it to stay level, that should have been (50, 0, 50)

This discussion is preserved as it was. New answers, comments, and votes are closed.