How do I learn Raycasting?

I know ROBLOX Documentation exists. The title explains what I would need help with. Thanks.

I wanna make a laser beam gun.

No one explains everything at the same time, like Magnitude and other stuff.

4 Likes

Read the roblox documentation. Raycasting | Documentation - Roblox Creator Hub

5 Likes

bild

5 Likes

There are plenty of posts about shooting a gun with raycasts, or raycasting in general.
No, they don’t explain it beginning to end but they give you good ideas of where to start.
Did you try the example place they show in the raycasting documentation?

4 Likes

Read this post, but this is insanely long so be wary of that

I also didn’t write this either so I can’t fix the mistakes

3 Likes

I tried all posts Roblox documentation has, like most of them for Raycasting.

2 Likes

What about just googling it? This is what I found. It’s from 2020 so it may be a bit outdated.

1 Like

Of course I have alreadt done it.

2 Likes

I will explain Raycasting to you. but before you read, I have limited knowledge in Raycasting, so I may miss some parts

The way you do raycasting is this:

workspace:Raycast(origin: Vector3, dir: Vector3, params: RaycastParams?)

The origin parameter is where the start of the invisible laser [aka Raycast] will begin.
the dir parameter is the direction the raycast will go to.
the params parameter is the RaycastParams it will use, For RaycastParams to be created, you would use this

RaycastParams.new()

The FilterDescendantsInstances is the Instance Filter. It uses table, but there is also AddToFilter which I will explain later
The FilterType is the Raycast Filter Type, It will either Exclude or Include the FilterDescendantsInstances, It currently accepts Exclude and Include, Their older counterparts are Blacklist and Whitelist, but as they once said “if it ain’t broke, don’t fix it”. but Roblox doesn’t follow that obviously. Here is the table for comparison

Old New
Blacklist Exclude
Whitelist Include

the IgnoreWater will detect if the raycast will ignore the Water terrain
the CollisionGroup is the specific target group for the raycast process with Collision Groups
the RespectCanCollide will detect if the casted part will be passed through if CanCollide is false
the BruteForceAllSlow will brute-force check every part, It will impact performance

the AddToFilter(instances: Instance | {Instance}) is like table.insert() but for FilterDescendantsInstances, It’s better to do that as you can insert instances to the list without making a variable and use table.insert()

3 Likes

In addition to what @VSCPlays said, some important things to note are:

  • When casting a ray, the distance between the origin and directional [Vector3] is the functional length (magnitude) of the ray. The maximum length is 15,000 studs.

  • workspace:Raycast() returns a RaycastResult, which has the following properties:
    Distance: number
    The distance between the ray origin and the intersection point.

    Instance: Instance
    The BasePart or Terrain cell that the ray intersected.

    Material: Material
    The Material at the intersection point.

    Position: Vector3
    The position of the intersection between the ray and the part.

    Normal: Vector3
    The normal vector of the intersected face.

3 Likes

The first two arguments in a raycast are the most basic ones.
Argument one is the origin, vector3 value, starting point of the ray. Meanwhile the dir/direction argument is also a vector3 value and it’s an orientation, not a position.

if a raycast intersects with something, it returns these, not all are listed: an instance which is the part it intersected with, position, which is the exact position the raycast hit at, and distance, which is the Origins position subtracted by the Positions position and converted to Magnitude.

For example, if you were casting a ray to the mouse’s position, you would subtract the mouse’s position minus the origins position.

workspace:RayCast(origin,(origin-targetpos))

TargetPos is the position that the mouse is at for this example.

2 Likes

Not reading that again, I need to recover getting braincells back.

3 Likes

he spent 50% of his braincells to write it, but you can read my explaination

also thank you for the quote, I inspect elemented it so I can reference it if I watch a cringe youtube video

1 Like

since people on this platform think its cool complicating RAYCASTING and making it seem like its some kind of achivement to know it, here is the simplest old way to use raycasting, although the method is deprecated, it can easily be replaced with new if you know the basics, im not gonna revamp it to new since im too lazy to do it and i wanna just copy paste lol

function RayCast(StartPos, EndPos, Distance, Ignore)
	local rayCast = Ray.new(StartPos, CFrame.new(StartPos, EndPos).LookVector * Distance);
	local Hit, Pos, Normal = workspace:FindPartOnRayWithIgnoreList(rayCast, Ignore);
	return Hit, Pos, Normal;
end

Example Usage:

local Hit, Pos, Normal = RayCast(HumanoidRootPart.Position, HumanoidRootPart.Position - Vector3.new(0, 1, 0), 10, {workspace.your_character_folder_storage_here})

everything inside ignorelist will be ignored, of course there is whitelist version and for whitelist just make it findpartonraywithwhitelist
the example script checks if the player has solid ground under him.

to make it work with a laser beam it would simply be

LocalScript:
someremotethinghere:FireServer(MousePos)

ServerScript:

someremotethinghere.OnServerEvent:Connect(function(Player, MousePos)
local Hit, Pos, Normal = RayCast(StartPos, MousePos, Range, {workspace.char_storage_folder, workspace.mouse_ignore_storage_folder_here_or_something )

HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, Pos);

this script makes character look at mousepos and just config it however you want
end)
1 Like

Magnitude is the measurement of distance in 3D space from the origin (0, 0, 0).

Raycasting is a pretty simple concept. Find an origin, a direction, and a distance, then shoot a beam from that position, in a direction. Except we are shooting this beam in 3D space.

Raycasting is simply done via the method workspace:Raycast()

This method/function takes a total of 3 parameters.

  1. The first one is your origin-- start position of your beam.

  2. The second one is your direction. Direction must also include the distance!

  3. The third one is your filter, which is not important for right now.

To obtain a direction and distance, think of 2 boxes. 1 Box is placed a bit far, the other is not. If we subtract the space between, we get a distance between the two, but you also get a direction between them. Below explains a vectors components visually.

Knowing this information. We can conclude that our vector obtained via subtracting the position of both cubes, is our direction and distance!

Lets test it out.

local Cube1 = Vector3.zero
local Cube2 = Vector3.new(5, 2,1)
local Difference = Cube2 - Cube1
local result = workspace:Raycast(Cube1, Difference)

--[[ Lets say I want to multiply that direction/distance by 2

local Cube1 = Vector3.zero
local Cube2 = Vector3.new(5, 2,1)
local Difference = Cube2 - Cube1
local result = workspace:Raycast(Cube1, Difference * 2)

-- [[ Now the direction/distance is multiplied by 2. So the distance has doubled.

The developer documentation states what will be returned from the variable result. It’s up to you to research that.

2 Likes

Anyways, lets say you want to raycast upward from a position.

local Origin = Vector3.new(0, 0, 0)
local Direction = Vector3.new(0, 1, 0)
local Result = workspace:Raycast(Origin, Direction * 4) --[[ Remember that's distance we just added to the direction

In that example we took our Direction and multiplied it by 4, which is our distance.

Let’s say we want to specifically obtain a direction from a vector which includes a distance.

local Me = Vector3.new(2, 5, 2)
local You = Vector3.new(5, 5, 5)
local Direction = (You-Me).unit --[[ This basically takes distance away from the vector. Resulting in the  direction ]]
local Distance = 20

local result = workspace:Raycast(Me, Direction * Distance)

This is called a unit vector, which is a vector that describes a direction instead of a distance. Hence the .Unit I added after local Direction = (You-Me).unit

We then added that distance back onto the Direction by MULTIPLYING it by a scalar number, which is distance.

simple version:

local PointA = Vector3.zero;
local PointB = Vector3.new(40, 40, 40);

local Magnitude = (PointA - PointB).Magnitude;

Magnitude is distance between 2 points, .Unit returns the direction of the points basically cba to explain it too much but just experiement

to make the laser size the same as the magnitude do:
Part.CFrame = CFrame.lookAt(ShootPosition, RayCastPositionFromEarlierExplanation)
Part.Size = Vector3.new(Part.Size.X, Part.Size.Y, Magnitude)

note that its just basics and you might have to change some stuff
Magnitude is basically distance between two vectors

I wasn’t referring to Roblox documentation. I was referring to searching the forums for other peoples posts.

The post that I linked about How to make a raycasting gun is a step-by-step tutorial, including the scripts that are needed.