CFrames (Coordinates, ToWorldSpace(), LookVector) - Roblox Advanced Scripting #15 — Transcript
Full transcript
- 0:00for some reason there's this part behind me that just for some reason keeps on following
- 0:04me wherever I go so if I walk in any direction no matter where I go this part literally keeps
- 0:11following me behind my character specifically behind my back and it's really crazy how it's
- 0:18able to determine exactly where to position itself and where to orientate itself while it keeps on
- 0:24updating and following my character behind my back it's actually really interesting let's see what's
- 0:28happening behind the scenes here all right let's think about this for a minute if we open up the
- 0:33script that basically is managing this part's uh position behind my character right now uh let's
- 0:39see what's going on inside of here uh okay let's actually get rid of this part this part is just
- 0:44Irrelevant for what we're trying to discuss here but yeah basically this entire thing is being run
- 0:49by only four lines in this one single while true do Loop what is happening here exactly so we have
- 0:55the part and we have this property here called A C frame and we're updating the C frame every 0.01
- 1:03seconds to follow the C frame of our characters uh torso and it's using this weird method called two
- 1:10World space with this little weird constructed cframe right here uh that's constantly updating
- 1:16every single time don't worry if you don't know what any of this means right now but I just want
- 1:20to point this out that what we're doing here with our character is we're updating the cframe of this
- 1:28part to constantly be behind my character's back and it's one of those data types that's really
- 1:34really important to know about for if you're trying to position and Orient 3D objects inside of
- 1:40the workspace and that's basically one of the key definitions of using C frames it's to position and
- 1:45Orient 3D objects inside of the workspace and we can do this with very precise calculations uh to
- 1:53help us understand the 3D World more by doing this and that sounds a lot like vector 3s I'm going to
- 1:59to explain what the difference between CF frames and Vector 3s are in a little bit but with that
- 2:03being said now that we have a general idea of CF frames let's go ahead and start implementing it
- 2:08now that we understand it let me quickly reiterate what a cframe is a cframe is essentially a data
- 2:13type that acts as coordinate frames that allows us to rotate and position a 3D object inside of
- 2:22our workspace so this sounds pretty similar to uh Vector 3 when we're trying to change the position
- 2:27and orientation of a part but think of C frames as a combination of position and orientation at
- 2:35the exact same time under a new data type that Roblox has created which of course is called
- 2:40CF frames so let's say I inserted so let's say I inserted a part here inside of the workspace
- 2:47and remember that we have three uh axes inside of the workspace we have the Z axis then we have the
- 2:54x axis and then the Y AIS we have three directions that we can uh pick from from when we're modifying
- 3:02any object or part inside of our workspace and we can pretty much do the same thing when we rotate
- 3:06the part as well we have three axes to work with when we're rotating the part in whatever direction
- 3:12we're trying to um change it to and so when we essentially combine rotation with position we get
- 3:20C frames and that's how we're able to make really precise really complicated object modification uh
- 3:27inside of our 3D workspace so that is basic what I'm going to be showing you how to do inside of
- 3:31this video and so now that we understand that let's get to implementing it so since we have
- 3:35this part let's go on the right side and let's rename this part to cframe part and let's also
- 3:41change the size of this part by to 4 by 4x4 and then let's change the brick color to probably some
- 3:50sort of blue like really blue all right so now we have this part and also what we're going to do is
- 3:55make sure to check the anchored property as well so now that we have that what we're also going
- 4:00to do is change the position of this part to its origin which is going to be 0 0 0 so you can find
- 4:07that under here in the transform C frame property right here and now it should be over here so now
- 4:12that we have this let's actually try to change its coordinate uh let's try to change its cframe so
- 4:21once again a cframe can basically manipulate the position and rotation or orientation of whatever
- 4:28object we're trying to work with inside of the 3D workspace so let's say I wanted to move this
- 4:32part away from the origin and let's say move it 25 studs in the Z direction right here what we would
- 4:40do is we would create a new cframe object that we're basically going to apply to this part's
- 4:46current CF frame so that we can update it to be inside of this cframe rather than where it
- 4:50originally was over here so what we're going to do is we're going to create a script by going to
- 4:54the right side hitting the plus sign in front of the part and then we're going to hit script so
- 4:59so let's delete this let's make reference to our part by saying local part equals script.
- 5:03parent and so now what we're going to do is we're going to create a new cframe object and the way
- 5:09this is going to look is this is going to use a Constructor for it so we're going to say local
- 5:14new cframe equals cframe do new open and close parentheses and you remember how for the position
- 5:22coordinates that we're trying to uh change we're just saying that we want it to be at 0025 as the
- 5:28new position so when we create a new C frame one of the ways we can create this is by simply
- 5:34putting in the three XYZ position coordinates that we want to give it so it's going to be 0
- 5:40comma 0 comma 25 so this is going to be the new C frame that we want to update this part's currency
- 5:47frame to uh by doing it by doing so down here so let's drop two lines and now all we need to do
- 5:53is say part. cframe equals new cframe so this is pretty much everything we need to do in terms of
- 6:00that let's go into the game and let's hit test and hit run to see our result so as you can see when
- 6:07the game ran uh we changed the parts C frame to the new positional C frame that we made it out to
- 6:13be being the position 0025 now of course this is one way of creating a C frame it's just by adding
- 6:20in its uh positional coordinates to whatever the new one is we're trying to uh set the part to but
- 6:25there's many other ways we can uh utilize this new cframe Constructor to do other things like change
- 6:33its position but also change its orientation to create more complicated uh calculations with this
- 6:39sort of stuff but this was just a basic example and I hope you understood this now let's say
- 6:43we wanted to change the orientation of this part using C frames and not by changing the orientation
- 6:48property itself so I hope at this point you remember how rotating 3D objects work at least
- 6:54inside of a script so we basically start at the origin at 0 degrees and if we want to update the
- 7:00orientation of a part then we have to specify the angle that we want to change uh whatever axes we
- 7:07want to change the orientation to so we start at zero and then when we go clockwise it's going to
- 7:12rotate like like this every 45 degrees or however many degrees you want it to uh until it goes all
- 7:19the way around going back to zero again or 300 60° in this case um so with this in mind we're able
- 7:27to create a C frame and change this for ourselves inside of the scripts that we're trying to change
- 7:32the parts orientation to and I'm going to show you what that looks like using CF frames so let's
- 7:38go inside of our script and what we're going to do is we're going to comment out this uh previous new
- 7:44C frame and we're going to create a new uh cframe right here and what we're going to do on the right
- 7:49side is we're going to use a method from cframe called cframe do angles and this is basically
- 7:55what it's going to look like so we're going to say cframe do angles make sure it's in uh it's
- 8:01uppercase and then open and close parentheses so now what we have here is is an orientation method
- 8:08that we're able to pass down uh three arguments here uh for the degrees that we want to to the
- 8:14desired orientation that we want to give it now here's the thing angles by default is in
- 8:19the units of radians and if you don't understand what radians are they're basically a different
- 8:24metric of orientation uh compared to angles so those are like two separate so those are like
- 8:30two separate units of measurement by default it's in radians so what you want to do instead of using
- 8:36radians is convert our degrees into radians using a method called math. rad so let me show you what
- 8:44this looks like we're going to say math. rad open and close parentheses and then put in our degrees
- 8:52that we want to put inside of here which is in this case going to be 45° because as you know
- 8:57if we go 45° then it's going to look like this so this is what we want to H so this is what we want
- 9:03the script to do but this is in the Y AIS not in the x- axis so we have to make sure that there's
- 9:10a zero before uh whatever is here in the middle so let's make another comma and then put in zero
- 9:17so this is basically our new orientation C frame exclusively this is specifically for orientation
- 9:24not for changing the position uh of the C frame this is one way of writing the the new orientation
- 9:31C frame so let's go into the game and hit run to see what happens all right and as you can see the
- 9:36part is now rotated 45° on the Y AIS if I open up the properties for this part we can scroll down
- 9:44and see that the orientation is now successfully at 045 for the new cframe that we updated using
- 9:51CF frame. angles so I hope that made sense to you there's one really cool feature about CF
- 9:56frames that we can use and that is making a C frame that allows a part to look directly
- 10:02at a specific position that we uh give it uh and basically what we're going to do in this example
- 10:08is we're going to use our current part to make it look directly at another part's position um
- 10:16when we apply the C frame to our blue part here so what we're going to do is we're going to move
- 10:21this blue part alongside this direction so that we can actually see it better uh let's also add
- 10:26in a decal in front of this part so that we know that the front is facing in the right direction
- 10:32uh and what we're going to do is we're going to go to the right side and let's rightclick
- 10:37duplicate and let's move the part over here and let's rename this part to look part and let's
- 10:45also change the color of this to probably like red or something and let's open this up let's
- 10:50get rid of the script that's inside of here and now let's go back to our current script so inside
- 10:55of our current script what we're going to do is let's uncomment out this part here and let's also
- 11:00comment out this too so what we're going to do now is make reference to the the look part we're going
- 11:06to say local look part equals workspace colon wait for child look part and basically what we're going
- 11:15to do is we're going to create a new cframe that has two things uh included with it so the first
- 11:21one is its starting position and the second one is a vector 3 that specifies the position of the
- 11:28part that we're trying to look at so what this will look like is we're going to first write our
- 11:34uh starting position Vector our starting position also known as a vector 3 value we're going to say
- 11:40local start position equals part. position and then what we're going to do is we're going to
- 11:48specify the position that we're trying to make the the part look at so this is where look part comes
- 11:54in where we're going to say local look position equal equal look part. position and so what we're
- 12:02going to do now is we're going to drop two lines down here and now we're going to create the new
- 12:07cframe that we're going to apply the part to we're going to say local new cframe equals cframe do new
- 12:14and this is going to be another way we're going to write this so the first argument we're going
- 12:17to put in is our starting position so we're going to copy that and paste it into here and then it's
- 12:23going to be another Vector 3 value which is going to be the look position of our look part so we can
- 12:29put that in here and this is basically our new CF frame right here so if we drop two lines down
- 12:34here say part. cframe equals new cframe we now have our desired result for what we're trying to
- 12:42do we're first giving it a start position of this part right here which is basically just where it
- 12:47currently is right now like right now the position of this is 1826 it's going to start here but then
- 12:54we specify a look position that is basically saying we want this part to look exactly in
- 13:00this direction to where the to where this part's position is currently at right now I hope that
- 13:06makes sense to you let's go into the game and hit run so that we can actually see it for ourselves
- 13:11and there you go the part is basically in the exact same position as as it was and it's now
- 13:16looking directly at the position that this part is in so this is a really neat feature we can uh
- 13:22use with C frames and so I hope this part has uh allowed you to understand um another useful
- 13:27feature about CF frames another thing we can do is combine multiple CF frames together using cframe
- 13:34operations so basically if let's say we were currently here in this position at 1826 with this
- 13:41part let's say we wanted to to take it to where it currently is right now to let's say um - 255 - 255
- 13:50studs in the X Direction here so how can we do that how can we start from here where it
- 13:57currently is to offset it 25 um studs this way rather than starting from the origin what we can
- 14:03do is we can combine C frames together to get the result that we want to when we're offsetting uh a
- 14:10parts C frame so what we're going to do is we're going to go back into the script let's comment
- 14:15out everything we've done up until this point so far and let's drop two lines at the very end down
- 14:21here so what we're going to do is we first need to get the current C frame that the part is already
- 14:27in right now now so what we're going to do is we're going to say local part position equals
- 14:34part. position next thing we're going to do is we're going to create a new C frame using the
- 14:41using the information that we have here with the current part position plus a new Vector 3 value
- 14:47in order to make this work for us so what we're going to do down here is we're going to say local
- 14:52new position equals part position plus vector 3. new open and close parentheses -25 comma 0 comma
- 15:040 and finally what we're going to do is we're going to set the parts cframe so part. cframe
- 15:11equals a new cframe because what we have right now is a combined Vector 3 value what we're going to
- 15:17say is cframe do new open close parentheses new position you have to make sure that you wrap this
- 15:24new position inside of a C frame otherwise we're going to get a mismatch error because we're not
- 15:29trying to make this C frame equal to a vector 3 that's why it's very important to change this into
- 15:35a C frame okay wait I meant uh 25 not negative 25 so let's go into the game and hit run so that
- 15:40we can now see the change that's going to happen with this part so there we go we basically started
- 15:44from where it currently was and we added 25 to its x value so that it now went inside of this
- 15:51direction um rather than starting from the very origin It Started from where it currently was to
- 15:57give it a offset so that it can move to the new position that we uh specified with the C frame so
- 16:02that's pretty much how you make an offset when it comes to changing a parts position all right but
- 16:07here's also another important thing that I should note here is that what we did was that we added
- 16:13two Vector 3 positions and created a new C frame off of this but what if we wanted to make it so
- 16:20that we literally take the C frame of the part directly and we added a new C frame on top of
- 16:26this C frame so there's another way of doing this rather than having it like this uh let's comment
- 16:31out this part so I can show you another way of doing it so what we're going to do down here
- 16:35is simply say part. cframe equals part. cframe so we're literally just taking the exact cframe
- 16:43that this already is in and we're going to add another C frame on top of it but here's the very
- 16:49important thing to know about we're not adding C frames necessarily we're multiplying CF frames
- 16:54to other CF frames and that's essentially how you quote unquote add C frames it's a very important
- 17:00distinction to know about with C frames that are not the same way of doing with Vector 3 values so
- 17:07here we can create a new cframe um data type to add on top of the part cframe by saying CF frame.
- 17:15new open and close parentheses 25 0 and this right here basically acts exactly the same way as we did
- 17:22up here rather the difference here is that we combined C frames rather than combin in Vector
- 17:293 values so if we go into the game and hit run we should get the exact same result as as we did
- 17:34when we combined uh positional values rather than cframe values so I hope this made sense to you as
- 17:41well uh like I said one important distinction to know is that when you're combining C frames you
- 17:46use the multiply or the asterisk symbol instead of adding it because adding is a vector 3 property
- 17:53not a cframe property all right so this next part is going to be a little complicated so I
- 17:58I want you to fully pay attention to this and I'm going to try to explain this to the best
- 18:02of my ability um but so far up until this point we've basically been manipulating C frames of 3D
- 18:07objects based around the world's perspective but we can't rely on the world's perspective
- 18:14every single time we're trying to change a um a C frame of a 3D object because let's say we had
- 18:20this part right here and we decided to rotate this part 45° on the z-axis I what we' want to
- 18:26do is if we want to further make changes to the C frame then we would have to make it relative to
- 18:32whatever the orientation and position this part is already in because now ideally the X Y and Z
- 18:39axis should be facing this way relative to uh what the orientation position already is but that's not
- 18:46the case here because if we're doing this based solely around the world's perspective the X Y and
- 18:53Z axis is still looking like this but what we want to do is make it look like this instead relative
- 19:00to the current position and orientation of the part oh and by the way um if you're wondering
- 19:05how I'm able to change the relative and Global axes I basically hit down control L I think
- 19:11it's command L on Mac but now what we're doing is we're setting up its local axes to be able
- 19:18to change this stuff but if we want to change it back we can just hit contr L again so now it's in
- 19:23the world's perspective and not the relative Parts perspective so I I hope that analogy made sense to
- 19:30you understanding the difference between local and relative or Global and relative C frames because
- 19:37that's what we're going to try to do with this um example we're going to try to change it based on
- 19:43its relative C frame not the global C frame so let's say we wanted to change the the relative
- 19:51CF frame of this to move neg five studs on the y axis in this direction what we're going to do is
- 19:58we're going to go to our script and we're going to use a method that cframe has provided to us
- 20:03called two world view so this is what it's going to look like let's drop two lines down here and
- 20:08let's say part. cframe equals part. cframe so we're taking the exact same C frame that this
- 20:17part is already currently in and we're going to use a method called colon 2 world space open and
- 20:26close parentheses and what we're going to here is we're going to pass in an offset C frame so
- 20:31basically if we started here and we want to move it in this direction by NE five studs on the Y
- 20:37AIS what we would do is we're going to create a new cframe and pass it into here as the offset
- 20:42cframe so what we're going to do is we're going to say cframe do new open and close parentheses
- 20:490 comma -5 comma 0 and now we have our relative position where the part currently is and we're
- 20:58going to give it an offset of5 on the Y AIS so that it can start where it currently was and then
- 21:04go ne5 studs in the y- AIS so let's hit run and see what happens I keep messing this up it's not
- 21:10negative five it's just a regular five so let's go into the game and hit run to now see your change
- 21:16so we were able to successfully change the Y AIS of its current C frame to be relative to where it
- 21:24currently was and not have it based on the world's perspective it was from the perspective of this
- 21:30part so I hope that analogy made a lot of sense to you and I hope you now understand the difference
- 21:36between Global and relative C frames because this is very very important when it comes to
- 21:41perspectives uh when changing CF frames C frames also have these neat properties that allow us to
- 21:48determine the direction that a part is facing and we can actually use these properties knowing which
- 21:53direction that the part is facing to create accurate calculation to the position of the
- 22:003D object that we're trying to manipulate so there's three properties uh that I'm going to
- 22:04introduce in this video uh the first one that I'm going to introduce is look Vector the second one
- 22:09being right vector and up Vector but the most important one I think you should know about is
- 22:14the look Vector so as you know 3D objects have different faces so we have a front face a back
- 22:21face a top and you basically get the idea of the rest so when we're determining the look Vector
- 22:27it's basically the direction that the front of the part is facing so in this case where this little
- 22:32decal is this is the front of the part and it's facing in this direction so it basically returns
- 22:38or look Vector in this case returns a vector 3 telling us which direction this part is facing
- 22:44and we can use this property to then say okay what if this part is over here and we want to move it's
- 22:50and we want to move this part in wherever this look direction is and make it go in this direction
- 22:56based on however many studs we SP specified using a multiplier that's what we're going to do inside
- 23:01of this script using it look using its look Vector property so inside of the script let's comment out
- 23:07this part right here and let's drop two lines so in order to know the property of the part
- 23:12look Vector what we're going to say is local look Vector equals part. cframe DOL vector and that's
- 23:22basically what this is right here it's a vector 3 value that consists of zeros and ones that tell us
- 23:28um which direction inside of the world that this part is currently facing so what we're going to
- 23:34do now is we're going to take the part's current position and specify its look vector and use a
- 23:41multiplier to then tell us to make it go in this Direction with however many studs we decide to
- 23:46give it because if it's facing in this direction it's going to give us a value of one and we can
- 23:51use a multiplier to specify how many studs we wanted to move this way I I think you'll see what
- 23:56I mean when we actually get to scripting this so let's go back to our script let's drop two lines
- 24:00down here and how this is going to work is we're going to say part. position equals part. position
- 24:09plus and then we're going to wrap this around in parentheses we're going to say the look vector
- 24:15and now what we'll do is we're going to add in a multiplier for this to this look vector by however
- 24:21many studs we want to give it so in this case if we wanted to go let's say 25 studs this way then
- 24:27we're going to go back our script and we're going to multiply this by 25 so now we have the part's
- 24:32position B inside of its current position plus the look Vector that this part is currently looking at
- 24:39and multiplying it using however many studs we wanted to move inside of this direction so let's
- 24:43hit run and see our result so as you can see the part moved from wherever the part's Direction was
- 24:49looking with its look vector and it multiplied it by five studs going this way and this is basically
- 24:55the desired result that we got from specify if Ying its look Vector uh and having it go inside
- 25:00of the direction that we wanted it to go with the specified number of studs we can basically
- 25:06do the same thing with its right vector and its up Vector uh the right side being this and then the
- 25:12up Vector being the top part of the the object so we can go back to our script and instead of making
- 25:19this the look Vector we could just make this the right Vector so instead of look Vector we can
- 25:25just say right vector and then we can change the variable to right vector and we can change that
- 25:30like so over here and we can go into the game and hit run and this will basically do the exact same
- 25:35thing but instead of going in the direction of its look Vector it's going to go into the direction of
- 25:40its right vector and we can basically do the same thing with the up Vector like so and we
- 25:46can change the variable name here to up Vector and we can change that here so let's hit run and
- 25:51then what should happen is it's going to go up 20 25 studs in the up direction as expected like
- 25:58so so that is basically the introduction to some of the cframe directional properties that you can
- 26:04use to help you make accurate calculations when it comes to this stuff all right and so the last
- 26:09thing we're going to do in this video is change the C frame of an entire model because up until
- 26:14this point we were able to change the C frame of individual Parts but you might ask is this
- 26:19actually possible to change the entire CF frame of a model and to that I say technically yes it
- 26:25is possible but here's uh but here's some things we need to know about so if we let's say insert
- 26:32a model inside of workspace let's create a model inside of workspace and let's select this model
- 26:37the model does not have a cframe property in and of itself so you might ask how are we actually
- 26:44able to change the C frame of an entire model so pretty much how this works is that if we have
- 26:49a model full of parts then what we need to do is specify a primary part that's inside of this model
- 26:57so that we can change the C frame of that specific part to then change the parts of everything else
- 27:03with the C frame as long as we have some sort of constraint that's holding all the parts together
- 27:09it might not make sense to you right now but let me show you what I'm talking about so now
- 27:13that we created this model let's call this model um I don't know something dumb like my model I'm
- 27:18not very creative with this stuff and let's create two parts so this one's going to be the first part
- 27:23let's change this color to Blue let's duplicate this put it over here and let's change this one
- 27:27to Red so what we're going to do now is create something that's called a weld constraint that
- 27:33will hold these two parts together don't worry about not knowing what weld constraints are for
- 27:37right now uh you can just follow along what I do so when we hit the plus sign let's insert a weld
- 27:43constraint and let's click this and there's going to be two properties part zero and part one let's
- 27:48make part zero our current part and then let's make part one the other part that we're trying
- 27:53to hold together and now this is indicating that these two are are basically glued together let's
- 27:59take these two parts put it inside of our model and let's specify our primary part so let's make
- 28:06this blue part our our primary part so we're going to rename this part to primary part and
- 28:12what we're going to do is Select our model and there should be this property called primary part
- 28:18inside a pivot let's select this and let's select our primary part so now what we're going to do is
- 28:24insert a new script inside of here by hitting the plus sign creating script so let's make reference
- 28:29to our model by saying local model equals script. parent and now what we're going to do is get our c
- 28:38is get our primary part C frame so basically the C frame of this blue part over here what we're
- 28:44going to do is get our current C frame which is local current C frame equals model. primary part.
- 28:53cframe so now that we have this let's change the cframe of the the specific primary part that we
- 28:59have in mind so what we're going to do down here is we're going to say model. primary part. cframe
- 29:06equals current cframe time cframe do new and we'll just throw in some random positional C
- 29:13frame right here by saying 0 comma 50 comma 0 all right so now let's go into the game and let's hit
- 29:21run to see our change with this new part okay so what ended up happening was that the C frame ended
- 29:28up changing to be 50 studs Above This Direction but because this part is not anchored uh we we
- 29:35saw that it literally fell down from the sky so if we were to change this to something else like
- 29:40uh I don't know 50 studs in the X Direction let's hit run again to see our change and as
- 29:45you can see even though we only changed the CF frame of one part we were able to change the
- 29:50C frame of basically the entire model because of weld constraints and also our primary part
- 29:56so that that is a really important thing to know about when it comes to changing the CF frame of a
- 30:02whole model rather than just one part so that is basically where I'm going to end it off here and
- 30:07I hope you learned so much from the cframe video because this is a really long video and I hope
- 30:13that you find a lot of value with this video for the future to come uh when you look back on this
- 30:19and apply what you learned from this video into your future projects because CF frames are one
- 30:24of those things where you will have to understand how these things work and this is basically going
- 30:29to be integral to the entire rest of your Roblox developer Journey if you're still confused about
- 30:35coordinates and how positions and orientations work inside of your Roblox games then I encourage
- 30:39you to watch my Vector 3 video that I will have right here for you to watch so go ahead and watch
- 30:44that if you haven't already but with that being said I will see you in the next episode take care
About this transcript
This page contains the full transcript of CFrames (Coordinates, ToWorldSpace(), LookVector) - Roblox Advanced Scripting #15 by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 5,886 words across 305 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.