I need help making a directional ray

When I make a ray come out of a part and go on for say 100 studs it asks for the direction in a Vector3
but how can I edit the Vector3 to always be forward relative to the parts orientation

I Believe this is what you are looking for:

Ray.new(Part.Position, Part.CFrame.LookVector).Unit*100 

Correct me tho.

The 2 Arguments in a Ray are its Position (Origin) and Direction, For Range you will have to Multiply the Ray

Position is our Origin for the Ray
LookVector is the way the Item is facing (For our Direction)
Unit is the Magnitude of Ray (with the Direction of 1)
*100 is the Range of the Ray

I think you meant to have that parenthesis in a different location:
Ray.new(Part.Position, Part.CFrame.LookVector.Unit*100)
You can ditch the .Unit though because LookVector is already a unit vector.

1 Like

so if I had something like Ray.new(Part.Position, Part.CFrame.LookVector).Unit*math.huge it would do the same thing but with infinite length

Pretty much, However I’m not really sure about Infinite Rays, Just set a Range of at least 999

if you put math.huge in place of 100 the raycast will stop working

1 Like
local Part = workspace.Part

local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterDescendantsInstances = {Part}
RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist

local Raycast = workspace:Raycast(Part.Position, Vector3.new(0, 0, -100), RaycastParameters)

This method of raycasting is newer, faster, and more accurate than Ray.new(). The origin is already specified in the first argument, this means that the second argument (the direction) will be relative to the part’s center.

Our second argument is a Vector3. To always raycast forward relative to the part’s direction you change the Z-axis to a negative number going however many studs forward you want the ray to be casted.

Keep in mind, this will always cast relative to which way the part is facing. A part has 6 faces, there is a top, bottom, left, right, back, and front face. The ray will always cast from the front face to 100 studs forward.

Ray’s Have always been accurate

1 Like

WorldRoot functions such as workspace:Raycast() are often more accurate than their deprecated counterparts (Ray.new) however this is a tiny difference that is negligible. The claims that they are faster, less intensive, and newer however are definitely true.

1 Like

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