Let’s say im 11 years old, im not dumb, but im also pretty stupid still. Explain to me all functions of raycasting, how raycasting works, different ways raycasting is applied, ect.
Rays are lines which have an origin and travel in a given direction. They have no width, so they are very thin, and are used in all sorts of programming and physics. Ever wondered why games with realistic lighting have Raytracing? That’s because the light itself is just many, many rays which act like light sources which imitate real lighting.
To begin with, our Rays have an origin; this is a Vector3 position. From here, we say how far we want our rays to go, with their direction vector3. For example, if I wanted my Ray to go upwards 10 studs, I would use a Vector3 of (0, 10, 0). Easy.
To create a ray, we do the following:
local ray = workspace:Raycast(origin, direction)
local exampleRay = workspace:Raycast(Vector3.new(0,0,5), Vector3.new(0,0,-5))
Here, we have a generic ray, in conjunction to an example ray, which starts at the Vector3 Co-ordinate (0,0,5) and moves back to (0,0,0). If you want to find the final location of a ray, all you have to do is add the direction to the origin.
Once we’ve cast our ray, it will either hit something and tell us about what it hit, ceasing to continue, or it won’t hit anything and will return nothing. Assuming we hit a part, we’ll be given a RaycastResult, which contains the following properties:
- Distance
- Instance
- Material
- Position
- Normal
I’ll briefly discuss the self-explanatory ones, such as distance, which states the length between the origin and the point of intersection. Next is Position, which is where the Ray hit the part. Third is Instance, which returns the specific part or terrain cell intersected. Finally, Material returns the material at the point the ray hit.
The only confusing one on that list is normal, however with a little diagram it is easy to understand.
Now, a ray may hit a part at any angle, and sometimes you may need a stationary angle from the wall. For example, all though you may not understand what this is, if you wanted to compare the vectors for the dot product, you would require the normal.
But what is the normal? Well, it’s just a vector which is perpendicular (at a 90 degree angle) to the part it just hit.
With your new found knowledge of Rays, the first place you may find yourself using them is to make a gun. If you would want to do this, you’d first have to find your origin and direction. The origin is easy, as that’s just the point at which the bullet will leave the gun. However, to calculate the direction, you’ll need to subtract the origin from the mouse position (this is due to some vector maths, which I’ve posted a picture to below). Imagine the direction as what you do to point A to get to point B, which in this scenario would be add 4 to the x and subtract 2 from the y.
theres a line, and depending on how you tell the script to draw that line, it does a bunch of different things.
thats about all i know :3 ive ben trying to learn raycasting myself and its mostly trial and error (and, when you get in too deep, a lot of math i dont want to do!!!)
I know in building systems, like build a boat, the placeholder uses raycasts to find the correct placement. Then you click and place. How would i go about doing that?
Im curious to know why you didn’t ask chatgpt this question?
While it is very smart and very advanced. AI can provide invalid information especially on any type of scripting.