# I don't understand raycasting

Okay, so this might sound a bit stupid but I don’t understand raycasting. I’ve watched lots of YouTube tutorials but I still don’t understand it. I basically want to check if there’s a part obstructing the ray, if so a function returns true or something like that.
Could someone please explain how this works?

1 Like

Hello , did you try to check some posts? If not then I’ll send you links to posts. I recommend reading them. https://developer.roblox.com/en-us/api-reference/datatype/Ray https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartOnRay
https://developer.roblox.com/en-us/articles/Making-a-ray-casting-laser-gun-in-Roblox

1 Like

Hi,

Fortunately Roblox handles a lot of the mechanics for raycasting for us! All we have to think about as Roblox developers is the position where the Ray will start, and the direction that the ray will travel.

As said before, Roblox does a lot of the complicated bits for us and have even included some functions that we can use to our advantage. Check out this article in the developer section: https://developer.roblox.com/en-us/api-reference/datatype/Ray

We can find the closest point by using Ray:ClosestPoint() for example, and if this returns nil then the Ray extends infinitely before hitting something!

Hope this helps! -Tom

2 Likes

Will it extend infinitely or until it hits the destination?

The ray’s length is limited to 5000 studs

1 Like

If you want to dive into the specifics, when a ray is “cast” it simple takes a vector which starts at a given point (the origin) and extends in a given direction. Technically, the vector doesn’t actually exist but is calculated at very small intervals until it hits something - which means that if it never hits anything, the calculations could keep going forever meaning it would indeed be infinite.

If you’re interesting in how it works, try exploring some of the links to this Wikipedia article: https://en.wikipedia.org/wiki/Ray_casting (a quick warning that some of the stuff can get really complicated).

It uses a lot of mathematics, but everything is taken care of for us by Roblox. As far as a Roblox developer is concerned, yes the ray is infinite in one direction but not the other.

I’ve followed the steps provided in your previous reply and it prints Argument 2 missing or nil on the line where I check if ray:ClosestPoint() == nil.
Here’s my script; https://gyazo.com/5d7392f7d2d5a863ec35894a2f28c875
There needs something to be within the brackets, but idk what

I think I perhaps misunderstand myself what that function does, I remember create the same sort of thing when I was creating an AI that shot people when it detected them.

I found the model that I used and it seems that this would be a better suit: https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartOnRay

I’m sure you can read the documentation on that, it would also be helpful to read: https://developer.roblox.com/en-us/api-reference/datatype/Ray

After some coding I managed to make it work, kind of.
Is this code good or would you do it better?
https://gyazo.com/c55646d93b3d8f05555d2f9e2f406bf8

Here is how you raycast:

``````local ray = Ray.new(startVector, endVector)
local hit, position, normal, mat = workspace:FindPartOnRayWthIgnoreList(ray, {})
``````

If it works it works, the only suggestion I’d have is being careful with the Character element of the ignore list. This is because the character is technically a model object so doesn’t have a touch detector (as far I know), it may be safer to put the body parts (loop through the children of the character) into the ignore list.

But as I said, if it works it works.

1 Like

Also. In case you don’t know what a vector is. A vector is basically a position in 3D space (`Vector3.new(xAxis, yAxis, zAxis)`). Same with CFrames. Except CFrames have rotation, and stuff, while vectors are only 3D positions. (If I Am Not Mistaken!!!)

1 Like

A vector can also be the link between two points in an n-dimensional space. For example, the line between two points on a 2D graph can be expressed as a vector.

3 Likes