How does raycasting a direction work?

local part = workspace.Part
local raycast = workspace:RayCast(part.Position, part.CFrame.LookVector * 5)

This would shoot a ray from your part forwards by 5 studs right? But what if you wanted to move it upwards & forwards so instead of shooting in front it shoots in a slanted direction? How does the direction part of rays work?

local part = workspace.Part
local raycast = workspace:RayCast(part.Position, Vector3.new(0,0,-5))

Would this work as well?

To move it upward and foward, you can just offset the look vector by another vector.

local part = workspace.Part
local raycast = workspace:RayCast(part.Position, part.CFrame.LookVector + Vector3.new(0, 5, 0))

The direction of ray cast needs to be relative to the origin, so make sure you get that right.

local part = workspace.Part
local raycast = workspace:RayCast(part.Position, Vector3.new(0,0,-5))
Would this work as well?

No.

3 Likes

Oh I see, also say I was making a hitbox and I had 5 raycasts going upwards, downwards, to the right, to the left, and to the front of a part. Would I need to create 5 variables, or is there an alternative to make my code less consistent/spammy?

Also @SilentsReplacement why wouldn’t it work? Because when I printed the LookVector of the part it printed ‘(0,0,-1)’

It wouldn’t work because the part is not always looking at (0,0,-1) try rotating it and you’ll see it changes as the part is looking somewhere else.

Ah I see that makes sense, no need to test I get it now.