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

Ray distance Help?

Asked by 9 years ago

The wiki doesn't explain Rays very well and I'm trying to make a script that prints the distance of an object to what ever is intersecting the ray. Here is what I've got so far, no errors it just prints "0"

Part = script.Parent
local start = Vector3.new(Part.Position)
local lookAt = Vector3.new(start.X,(start.Y-100),start.Z)
local ray = Ray.new(start, (lookAt - start).unit)
print(ray:Distance(ray:ClosestPoint(lookAt)))

1 answer

Log in to vote
0
Answered by 9 years ago

When you make the direction for the ray a unit vector, it only has a magnitude of 1, so a part has to be really close to the ray origin for it to be detected by the ray. To make the distance of the ray longer, simply multiply the unit by any number that you want the distance to be, like this:

Part = script.Parent
local start = Vector3.new(Part.Position)
local lookAt = Vector3.new(start.X,(start.Y-100),start.Z)
local ray = Ray.new(start, (lookAt - start).unit * 50) --This will make the ray 50 studs long
print(ray:Distance(ray:ClosestPoint(lookAt)))

Hope this helped!

Note: The greatest distance a ray can be is 999. If you make the ray any greater than 999, the ray won't detect any thing past 999 studs. You'll also get a warning message in the output. It won't stop your script though

0
Well, now it just prints "624750016", and the part is 34 studs above the baseplate, and I want it to print the distance to the baseplate. PixelAspect 0 — 9y
Ad

Answer this question