Learning raycasting

What am I attempting to achieve?

I’m trying to learn ray casting and especially learn how to create beams and make parts gradually move along rays.

Issue?

I can’t figure out how I can implement parts along rays and overall turn ray casting into something more advanced.

What have I tried?

I’ve covered all the basics of ray casting such as params, origin, and direction(I am a little confused on how origin.Position - target.Position results in the direction). I’ve also read this community resource and videos from B Ricey, TheDevKing, GnomeCode.

Questions:

  • I am a small developer and very new, are there possible things I need to study over before ray casting?

  • What practices or specific things can I do to improve my skill in ray casting?

  • What resources can help me improve in ray casting?

3 Likes

What do you mean by parts moving along rays?

And for the direction, it’s Target Position - Origin Position. If you don’t understand how iyt works, simplify it on a Vector1 scale. If your Origin Position is 1 and the Target Position is 3, then you gotta add a 2 to arrive to the Target Position. So it’s 3 (Target Position) - 1 (Origin Position) = 2 (Direction). Same thing applies for Vector3.

3 Likes

We all start somewhere.

Raycasting is in the name itself; it casts a ray (an imaginary beam of light) in x direction. Roblox provides you with a beginner tutorial about raycasting. It contains everything you need to know.

For some sub-information there’s other tutorials on the developer forum, such as this one. I highly suggest you don’t dwell into complex systems; though I’m not stopping you. If you feel as though you could take the task, by all means, go ahead. However if you’re only beginning to comprehend raycasting, follow a basic pattern of “simple idea” → “simple problem” → “simple result”.


As I’ve stated above, raycasting is just an imaginary beam which travels in a certain direction. If it intersects with something, it’ll return a RaycastResult containing some information about how the ray intersected, what the ray touched, the normal vector of the ray hit, etc… The entire list of components is listed in the official documentation.

I assume you’re talking about trajectory. There is some valuable information on the developer forum about that. Especially YouTube.

Into something more advanced? I’m confused on what you’re trying to achieve. Keep it simple, don’t overcomplicate raycasting; it’s a simple line, travelling in a given direction.

Some valuable information would be how to raycast from the mouse, or from a point on a screen. Methods such as ViewportPointToRay and ScreenPointToRay. If you wanted to raycast from the player’s Mouse into the 3D world:

-- services
local userInputService = game:GetService("UserInputService")

-- variables
local currentCamera = workspace.CurrentCamera

-- functions
local function raycastFromMousePosition(origin: Vector3, raycastLength: number)
	local mouseLocation = userInputService:GetMouseLocation()
	local mousePositionX, mousePositionY = mouseLocation.X, mouseLocation.Y
	
	local screenPointRay = currentCamera:ScreenPointToRay(mousePositionX, mousePositionY)
	local direction = screenPointRay.Direction.Unit * raycastLength
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	
	local raycastResult = workspace:Raycast(origin, direction, raycastParams)
	
	return raycastResult
end


raycastFromMousePosition(Vector3.new(0, 30, 0), 1000)

Use RaycastParams properly. If you have a raycast only meant to detect enemies, make sure you use Enum.RaycastFilterType.Include, and have the FilterDescendantsInstances table contain all the enemies. I’m sure the documentation iterates over some good practices.

Follow good tutorials. Whether they’re videos, forum posts or threads, as long as you’re learning something.

If you have any additional questions, feel free to ask.

1 Like

K got it, I’ll remember that for next time, thanks!

1 Like

Dang, thats a lot of information! By turning ray casting into something more advanced I meant using it for more purposes. Thanks!

1 Like

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