Hi guys,
I’m making a gun script and I’m new to CFrames. I wanted to learn a bit about how to set the lookvector of a CFrame? I get that it’s read only, but if there’s a way around that let me know. I would just match the entire CFrame, but I have to match a bullet’s lookVector to a ray’s Direction Vector. Any ideas? Thanks!
Could use CFrame.new(point,pointToLookAt) So,
bullet.CFrame = CFrame.new(ray.Origin,ray.Origin+ray.Unit)
A really good way to look at and understand lookVectors can be found here (via Roblox Developer Hub)
So, imagine CFrames as a translation. It’s not really a coordinate system, it’s more of a way to show translations.
Think about this line of code:
-- Translates a Part up by the Vector (1,0,0)
Part.CFrame = Part.CFrame * CFrame.new(1,0,0)
That describes a translation from the original CFrame to the new final CFrame.
Now, you should think of all CFrames like this:
-- Translates a part up by Vector (1,0,0) from the base position
Part.CFrame = CFrame.new(0,0,0) * CFrame.new(1,0,0)
-- ... Which is the same as
Part.CFrame = CFrame.new(1,0,0)
So think of all CFrame operations as a translation from the root (0,0,0)
Now, when you think of these translations, let’s break it down into three vectors which describe that translation
The first is the vector going from the x coordinate. That’s called the rightVector.
The vector coming from the y coordinate to the new y coordinate translation is the upVector
And finally the translation vector between the z coordinate and the final z coordinate is the lookVector
Think of it like this:
(courtesy of the Developer Portal)
I hope that clears it up!
I tried out this code, but you forgot that ray.Unit returns a ray that has a “normalized direction” (according to the wiki) but ray.Unit.Direction will return the Vector3 we’re looking for
Thanks for making that post & linking the article, it’s a big help!