Are Rays useful?

Hi. I don’t see a lot of Rays in code, but today I saw some code with Rays in it. So, I wanted to know if it’d be useful for development. So far, it’s a bit hard to understand.

  1. What do you want to achieve? Knowing if Rays are useful or not.

  2. What is the issue? Trying to understand Rays are a bit hard, and I have no idea if I’m wasting my time by trying to learn them.

  3. What solutions have you thought of so far? Well, I originally tried making an Instance.new(“Part”, workspace) and putting the Part in a Ray position… but that didn’t exactly work out, as I didn’t even know where to start.

So yeah, this is basically all you need to know. An example of some code with Rays in it is in the Dev Hub. Take this for example:

local function createPart(position, processedByUI)
	-- Do not create a part if the player clicked on a GUI/UI element
	if processedByUI then
		return
	end

	-- Get Vector3 world position from the Vector2 viewport position
	local unitRay = camera:ViewportPointToRay(position.X, position.Y)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * LENGTH)
	local hitPart, worldPosition = workspace:FindPartOnRay(ray)

	-- Create a new part at the world position if the player clicked on a part
	-- Do not create a new part if player clicks on empty skybox
	if hitPart then
		local part = Instance.new("Part")
		part.Parent = workspace
		part.Anchored = true
		part.Size = Vector3.new(1, 1, 1)
		part.Position = worldPosition
	end
end

When I actually typed some of it in, I noticed a term or two that was actually deprecated. Which, to me indicates that it either isn’t that important, or has not been updated yet. Is this true? If you can help, please do. (IF THIS TOPIC IS NOT IN THE RIGHT AREA, PLEASE LET ME KNOW WHERE TO PUT IT, SO I CAN CHANGE ITS LOCATION.)

Using raycasts in games can be very good.

The method for raycasting in that script though is definitely old.
To my knowledge, this is how you would raycast now:

local RayOrigin = Vector3.new(0, 0, 0) -- The ray starts at 0 0 0
local RayDirection = Vector3.new(0, -50, 0) -- The ray goes down
local RayParams = RaycastParams.new()

local Result = workspace:Raycast(RayOrigin, RayDirection, RayParams)

Raycasting can be used for a multitude of things, one of them is hitscan guns.

For example, you want to check if something is on the screen and also check if it is fully occluded.
You would raycast to the 8 corners of the object and the center, if all of the raycasts don’t hit the object, then it is fully occluded and cannot be seen, however, if one ray hits the object, it can be seen.

So, in conclusion, yes, raycasts are very useful.

(Also, I mean, raytracing uses raycasting so yeahhhh)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.