Hello!
Being a bit more of a new member, I just was wondering, when someone gets free time, if you can just explain Rays to me. Thanks so much!
Hello!
Being a bit more of a new member, I just was wondering, when someone gets free time, if you can just explain Rays to me. Thanks so much!
Actually, is it kind of just like getting the distance of something?
Similar to that and more!
it can do alot of things you dont know about!
Ok, sweet! Thanks again!
No problem,you might need to study it or follow some tutorials,add me on discord
EnqryptedAPI#0837
This article is a great resource for all types of ray casting:
Imagine a line that starts at one point and extends infinitely in another direction. That is pretty much all that a ray is and Roblox provides a built in class and method to encapsulate this idea. Note that the class in Roblox actually does have a finite point which I will talk about in a second.
Ray.new(Origin, Direction) is the constructor for a ray. The Origin is a Vector3 position where the Ray will start. Direction is a Vector3 that tells you the direction the ray is pointing at and where it ends.
For example, say I have my Origin at (0, 0, 0) and my Direction at (1, 1, 1). The starting point for my ray will be at the Origin and it will extend +1 on every axis meaning the endpoint for this ray will be (1, 1, 1).
Another example is say I have my Origin at (1, 2, 3) and the Direction is (1, 2, 3). The starting point will be on the Origin but the endpoint will be at the Origin + Direction: (2, 4, 6).
Rays are very useful. One of the primary uses are when it comes to detecting objects. Roblox provides methods to return objects that are “seen” by the ray (objects that intersect with the ray). One way you would use this would be in a hitscan gun. You would cast a ray from the gun to the endpoint and then return if an object intersected that ray or not using FindPartOnRay().
This helped a lot! Thanks!
So, I tried this:
local ray = Ray.new(workspace.RayStart.Position, workspace.RayEnd.Position)
and it said:
function: 000001F670B76C20
Hmm thats weird as heck,why would you create a variable that has a End position
local raystart = Ray.new(workspace.RayStart.Position)
local rayend = Ray.new(workspace.RayEnd.Position)
Well, your not supposed to print a ray.
You can print the part the ray hits, though.
local ray = Ray.new(Vector3.new(0,0,0),Vector3.new(5,5,5)
local part = workspace:FindPartOnRay(ray)
if part then
print(part.Name)
end
This will check if there if the raycast hits a part from 0,0,0 to 5,5,5, and if it does, prints the parts name.
OOf,im not good with stuff like Rays,well im not the best at it,
Want to give me some tutorials on lua(Intermediate stuff)by any chance?
maybe another time, OK? Sorry.
Wym,your all fine,but you need to seperate variables for the start ray and end ray.or when you use that variable it will find the Start Ray and End Ray at the same time
A ray in Roblox has an end position, but you aren’t supposed to specify that in the constructor.
You put RayEnd’s position instead of the direction the ray should be looking at. It’s like telling a friend “Look a bird!” instead of telling him the direction and saying “Look, there’s a bird to the north!”
The correct way to implement the thing you want would be:
local RayStart = workspace.RayStart
local RayEnd = workspace.RayEnd
local Length = 10
Ray.new(RayStart.Position, (RayEnd.Position-RayStart.Position).unit*Length)
Roblox has a cool method called .unit which does all the annoying math for you and gives you a direction with length of 1. Now, if we want the ray to extend farther than 1, we want to multiply it by a number (Length). So the example will create an array starting at RayStart and extend 10 studs towards RayEnd.
Also, I’m not sure what you are printing but if you want to test this out, place a brick between RayStart and RayEnd and rename it to something like “Target”. After creating a ray, use FindPartOnRay to return the first part the ray hits and print out the name.
@MaximussDev you dont add RayEnd position and RayStart Position together
you make them two seperate variables
This thread is a bit of a mess, so here’s my explanation.
A ray in physics is a line which has an origin and a direction, but in Roblox it also has a length for performance reasons.
Rays in Roblox are basically userdata objects just like Vector3s or CFrames. On their own, they don’t do much and are only useful as temporary containers. Their only 2 methods are :GetClosestPoint(referencepoint)
which returns the closest point thst lies on the ray and :GetDistance(referencepoint)
which does the same, but returns a number representing the distance instead of a vector.
To create a ray, you must use the Ray.new function. Its parameters are the origin as a Vector3 and the direction also as a Vector3. The direction is basically obtained by either multiplying a unit vector representing the direction with a desired length (make sure it’s not too much as it can cause lag) or subtracting the origin from the desired end point.
As for the methods that make them really useful, all of them are under workspace:
workspace:FindPartOnRay(ray, instancetoignore, terraincellsarecubes, ignorewater)
This is the basic function for raycasting. It will ignore the specified instance and all of its descendants and the returned values are the part that got hit, tha hit position, a unit vector representing the direction the face that got hit is pointing at and finally the part’s material (important for terrain)
workspace:FindPartOnRayWithWhitelist(ray, whitelist, terraincellsarecubes, ignorewater)
Similar to the previous, but will only accept parts specified in the whitelist (a table with all the parts to detect). Can be used as an opposite function to FindPartOnRay by specifying a single instance.
workspace:FindPartOnRayWithIgnoreList(ray, whitelist, terraincellsarecubes, ignorewater)
The opposite of the above. Excellent for stuff like custom suspension as you can use it to ignore non-collidable parts by placing it in a loop and updating the ignorelist table.