If I have a part, and want to measure its "Studs per second" from its front face (and if it goes backwards the magnitude would be negative) could I do so with a lookvector even though that's a CFrame/vector term?
I am interpreting your question as this:
I want to know how fast a part is moving in the direction of its front face
Meaning this speed would be 0
if the object is traveling directly to its left, right, top, or bottom, but it would be 1
if it's traveling at 1 stud per second in the direction of its front surface, and -1
if traveling at 1stud/sec in the direction of its back face.
This is quite easy! This is essentially asking for a vector projection of part.Velocity
onto part.CFrame.lookVector
. When the vector you're projecting onto is a unit vector (which lookVector
is) this computation is very simple:
The velocity in the direction you're going forward is part.Velocity:Dot( part.CFrame.lookVector ) * part.CFrame.lookVector
. If you only care about the "size" of that (with sign), then you just need part.Velocity:Dot( part.CFrame.lookVector )
.
The dot product essentially measures how "similar" two vectors are. For two unit vectors u
and v
, u:Dot(v)
will the the cosine of the angle between them (meaning 0 when perpendicular, 1 when the same and -1 when opposite).
For two Vector3s u
and v
, u:Dot(v)
is the same as u.x * v.x + u.y * v.y + u.z * v.z