After a lot of more inquisition about these topics, I have decided to make another tutorial!
This is going to be the simplest explanation ever made of Cross and Dot, so make sure you follow to the end
By far, two of the most puzzling topics in scripting are Vector3:Cross() and Vector3:Dot(). However, after learning what they do, you will realize that they are actually 100x easier than using CFrames even!
You will also want to use them in your code when you realize what they can do, especially Dot()
Just to catch anyone up to speed, a LookVector is just the direction a CFrame is facing.
In this picture,
Block.CFrame.LookVector = Vector3.new(0, 1, 0)
Because it is facing directly up.
All Dot does is see how much 2 vectors go in the same direction.
If you are comparing how much in the same direction the LookVector of a Part and the LookVector of a Camera are going, it would look like this:
Here is what Dot will do for this:
local dotProduct = CamLookVector:Dot(BlockLookVector) -- dotProduct = 1
Because the camera and the block are facing in the same direction, the Dot of their LookVectors will equal 1.
What if the Block and Camera were facing in opposite directions?
local dotProduct = CamLookVector:Dot(BlockLookVector) -- dotProduct = -1
Because they are facing in opposite directions, the Dot of their LookVectors will equal -1.
What if they are facing at a right angle to each other?
local dotProduct = CamLookVector:Dot(BlockLookVector) -- dotProduct = 0
Because they are facing at a right angle to each other, the Dot of their LookVectors will equal 0.
Also take note that It does not matter the order you use Dot() in:
Always equal the same thing.
CamLookVector:Dot(BlockLookVector) = BlockLookVector:Dot(CamLookVector)
Real Examples of Using Dot
You attacking an NPC only if your character is facing it
A monster that teleports behind you only if you are not looking at it
Calculating how much an airplane is pointed forwards, perpendicular from the ground, in order to see how much drag in speed it should receive (If they are a perfect right angle, Dot will be 0)
That’s it for Dot!
If you skipped the explanation on Dot, make sure to go back and read it in order to understand what a LookVector is.
So since the LookVector is just the direction that a CFrame is facing, or Looking, the RightVector is the direction the right side of the CFrame is facing, and the UpVector is the direction the top side of the CFrame is facing.
Here is an example of what all three of them, the LookVector, RightVector, and UpVector look like on this rocket’s CFrame.
Don’t judge my rocket building skills
In a new scenario, let’s say we have both the UpVector and the LookVector, but not the RightVector. We need to find it.
We can find the missing RightVector by using Cross.
All Cross does is find a vector that is perpendicular to the other two vectors that you give it.
In this scenario, because the RightVector is perpendicular to both the UpVector and LookVector, it will work perfectly!
local missingRightVector = LookVector:Cross(UpVector)
We found it!
Now that we know that Cross will give us a vector a right angle from two other vectors, going back to that rocket picture:
We know that
local RightVector = LookVector:Cross(UpVector)
Cross is very useful for that case!
Are not the same !!!
In order to find the RightVector, we have to use
But if we wrongfully flipped it, it would give us the LeftVector, which we don’t want.
Taking a look at another case:
This time, we will use three random directions instead of the LookVector, UpVector, and RightVector.
Wait, where on Earth is the missing Vector C supposed to be now?!?!?
Since using Cross will give us a missing vector a right angle away from both Vector A and Vector B, it will say that Vector C is this:
If you don’t believe me, let’s check to see if this new Vector C is truly a right angle away from the other two vectors:
Yep! So there we have it, Vector C is a perpendicular to both Vector A and the Vector B!
That example used VectorB:Cross(VectorA) to find that VectorC, but if we wanted to get the other side from VectorC ( -VectorC ), we would do VectorA:Cross(VectorB)
(read what I said above a while ago about why the order of that matters)
Real Examples of Using Cross
Calculating the RightVector from any LookVector:Cross(UpVector)
Calculating the LookVector from any UpVector:Cross(RightVector)
Calculating the UpVector from any RightVector:Cross(LookVector)
Finding which axis to rotate a cannon to face a target
That’s it for Cross!
So that’s how to use Vector3:Dot() and Vector3:Cross()
Thanks everyone, and if you found this helpful be sure to leave a like and check out my other CFrame tutorials, especially if you want to learn more about these vectors I talked about in this tutorial.
Also, tell all of your scripter friends about this as so we can inform as many people as possible!