Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Completely confused with rays?

Asked by
funyun 958 Moderation Voter
8 years ago

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

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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)
0
...Still, It's a going up diagonally. I'm trying to get a completely horizontal line going across the sky. funyun 958 — 8y
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) BlueTaslem 18071 — 8y
Ad

Answer this question