It’s hard to visualize but I’ll try my best:
I’ll be first demonstrating question 1 and 3 because they kinda go together:
First thing, .Unit returns the same vector except it’s limited to 1 stud from the “origin”, and 1 stud is what what “Unit” refers to. It’s usually useful for when we want to easily specify a certain distance to cast a ray from:
print(Vector3.new(1000,1000,1000).Unit) --> 0.57735025882721, 0.57735025882721, 0.57735025882721
To visualize, let’s say I have this part, the pink face is the front:
So basically, .Unit takes a directional vector (which is the LookVector of this part for now) and limits it to 1 stud from the origin of the part (as I’ve said already):
Let’s create a visual representation of the ray’s direction based on the part’s LookVector
So this is the actual LookVector which is the front-facing vector of the part which is basically the equivalent of the part’s LookVector that performed .Unit on it. This is particularly evident when we multiply the part’s LookVector by 100, then do .Unit on it; it’ll return the exact same Vector3:
print(workspace.Part.CFrame.LookVector) --> 0,0,-1
---
print((workspace.Part.CFrame.LookVector * 100).Unit) --> 0,0,-1
Another thing is that the part’s LookVector is the same as it’s front-facing directional vector, so if we want to cast a ray directly from the part’s front-facing face (lol alliteration), we can do this by multiplying the LookVector by how long we want our ray to be:
So first, if we raycast from the part’s position and we use the LookVector, it will return nil because the “goal” part is 7.5 parts from the part’s origin and the LookVector is 1 stud long which means it won’t be able to hit the part:
(Also the representation of the ray is the white part)
print(workspace:Raycast(workspace.Part.Position, workspace.Part.CFrame.LookVector)) --> nil
However now, if we multiply the part’s LookVector by 10, it’s casting a ray that’s 10 studs long from the part’s origin in the direction of the part’s LookVector:
Note that the white part hit the other part because the ray we casted was 10 studs long (which is shown by the transparent white part)
If we move the part, it won’t hit the part because it’s out of the way from the part’s LookVector:
Ok so what if we want to cast a ray in the direction of the second part from the first part’s origin?
As you may know, casting rays aren’t as easy as an orientation, and that’s because orientational vectors can’t have lengths as they represent an orientation, not a direction
So to visualize, if we want to cast a ray from the part’s origin in the direction of the “goal” part, we can use this formula:
direction = goalPosition - originPosition
So if we plug that into our formula, we can visualize our ray:
local ray = workspace:Raycast(workspace.Part.Position, workspace.GoalPart.Position - workspace.Part.Position)
local p = Instance.new('Part')
p.Parent = workspace
p.Position = (workspace.Part.Position + ray.Position) / 2
p.Size = Vector3.new(0.1,0.1,(workspace.Part.Position - ray.Position).Magnitude)
p.CFrame = CFrame.lookAt(p.Position, workspace.Part.Position)
p.Material = Enum.Material.Neon
Now, what if we want to apply a max distance that the part can “see”?
We can use .Unit. If we want to apply a 1 stud “seeing” radius, we can do that by simply using .Unit and nothing more, as you can see, the “ray” is in the direction of the goal part but it’s only 1 stud long:
local ray = workspace:Raycast(workspace.Part.Position, (workspace.GoalPart.Position - workspace.Part.Position).Unit)
Now, if we want to apply a greater distance, we can just multiply the unit vector by some number:
local ray = workspace:Raycast(workspace.Part.Position, (workspace.GoalPart.Position - workspace.Part.Position).Unit * 10)
As we can see, it hits the part and there’s still a bit more that it could have reached:
Second thing, CFrame:Inverse() returns the inverse of a CFrame, it’s easy to visualize without orientation being a factor and it’s usually used for flipping something the position relative to one CFrame, but it’s also commonly used for undoing a multiplication of a CFrame (which I’m not very well versed in to be honest):
print(CFrame.new(100,100,100):Inverse().Position) --> -100,-100,-100
This is a good answer that explains it, it’s something I don’t understand thoroughly enough to educate someone on it:
Can anyone exlplain inversing to me, like CFrame:inverse, and etc? - Scripting Helpers.