I don't understand fully about Raycast

I don’t understand what the direction parameter takes. Can I use CFrame.LookVector for it?

1 Like

Direction is basically a simple vector.
It has to have a magnitude which determines the depth of the ray.

For example:

local DirectionUnit = Vector3.new(1,0,0)
local Magnitude = 5
local Direction = DirectionUnit*Magnitude -- would give the ray direction to cast on the right size with a length of 5 studs.

This can also be a lookvector.
For example:

local Direction = Part.CFrame.LookVector
local Magnitude = 5
local RayDirection = Direction*Magnitude
1 Like

Direction is basically distance (how far the ray goes)?

1 Like

As I know direction is a Vector3 value that represents the rays direction relative to source of the ray.
For example:

local Direction = Vector3.new(2, 0, 3) * 10

--The first parameter in the Vector3.new() represents X axis (Front).
--The second parameter in the Vector3.new() represents Y axis (Up).
--The third parameter in the Vector3.new() represents Z axis (Left).

Image below can explain better than the snippet.

  • Red represents the line of collide.
  • Pink represents direction of the ray.

Too explain your second question, you have to know CFrame.LookVector is also a Vector3.
So you can use LookVector but you have to multiply it.

local Part = workspace.Part
local LookVector = Part.CFrame.LookVector
local Magnitude = 5

print(LookVector * Magnitude)

-- Magnitude means the ray will be 5 studs lenght because the LookVector will probably
-- return something like Vector3.new(1, 0, 1) or anything between 0 and 1.
-- so if you multiply a Vector3 then it will multiply all the cordinats and give a new Vector3.
-- then the direction will be Vector3.new(5, 0, 5).

I hope this answers your question

2 Likes

Direction is the vector that gets added to the origin, multiplied by the distance.

If the origin is (0,0,0) and the direction is (0,1,0) with a distance of 10, then we do (0,0,0) + (0,1,0)*10
This results in (0,10,0)

You can most certainly use the lookVector, but you should multiply it by something like 100, or your ray will be like the boys when it’s cold outside: very short indeed.

2 Likes

Yeah it’s just the offset of the goal from the origin.

1 Like

It isn’t. It’s a direction. Vectors ARE directions. They’re arrows with a length. If you have (10,0,0), then what this literally means is an arrow pointing from 0,0,0 to 10,0,0.

And although I agree with offset being an appropriate name, that doesn’t make direction misleading.

1 Like

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