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