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

Raycasting: Direction Value?

I have been looking at some wiki pages on Raycasting, and I have failed to understand how to direct the ray where you want it. I know that to make a ray, you do something like this:

local ray = Ray.new(startposition, direction?)

The thing I don't understand is what to put to make the ray go in the way I want. Do I have to put another point along the path I want it to go on?

Also, I am wondering if/how it is possible to send a ray in a certain direction based on a mouse click on the screen.

Thanks in advance!

1 answer

✓ Accepted answer

The direction is equal to (endpoint-startpoint).unit * length. Subtracting the starting point from the ending point results in a vector pointing in the correct direction, and .unit normalizes that vector so that all components are less than or equal to one. To create a ray from any point to the mouse's target, simply set the endpoint in that formula to Mouse.Hit.p.

2 comments

2

You'll also have to remember that if you don't change the length of the direction unit, and you keep it as a unit, the Ray will only detect things 1 stud away from the origin

0

To elaborate on TurboFusion's answer: While not documented, "direction" really is the direction and DISTANCE that a raycast will look in. A length 10 vector will go 10 studs, etc, so usually very large, like 1000, vectors are necessary to make it useful.

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