How to use CFrame


  • So, I have seen a bunch of people use CFrame incorrectly. So, I wanted to make a tutorial I could link to fix their problems. The most common way I see it used incorrectly is:

    #1, which is using CFrame.new(Vector3.new(10, 1, 100)). It is CFrame.new, not ConvertToCFrame. If you want ConvertToCFrame, do this:

    local function ConvertToCFrame(value)
    	if typeof(value) == typeof(CFrame.new()) then
    		return value
    	elseif typeof(value) == typeof(Vector3.new()) then
    		return CFrame.new(value.X, value.Y, value.Z)
    	else
    		return false
    	end
    end
    

    #2 People forgetting CFrame.Angles needing radians. There are two easy ways of getting radians.

    #1: Using math.pi
    To use math.pi, it is really simple. Math.pi is 180 degrees in radians. To get a 180 degrees flip, do

    game.Workspace.Part.CFrame = CFrame.Angles(0, math.pi, 0)
    

    To make a 90 degrees flip, do math.pi / 2
    To make a 260 degrees flip, do math.pi * 1.5
    To make a 360 degrees flip, do math.pi * 2.

    #2: Using math.rad
    math.rad converts degrees into radians. Just put the degrees in the parameter of math.rad, for example

    game.Workspace.Part.CFrame = CFrame.Angles(0, math.rad(90), 0) --will turn part 90 degrees
    

    I think this explains itself, just insert the degrees into math.rad.

    Side note:0 degrees is 0 radians, so don't waste your time doing math.rad(0).

    Hope this helps with your CFraming!


  • @hiimgoodpack

    local function convertToCFrame(x)
       return typeof(x)=="Vector3" and CFrame.new(x) or typeof(x)=="CFrame" and x or false
    end
    

    (This function is pretty useless I think but this is just a shorter way of writing it)

    (not tested)


  • @CootKitty I don't like messy code that do multiple things on one line because I have to scroll to see the code I did.


  • @hiimgoodpack,
    CFrame.new(value.X, value.Y, value.Z

    ^^^^^^ Kinda forgot the ")" at the end :3


  • @greatneil80 no i didnt


  • There is nothing wrong with CFrame.new(Vector3.new(10, 1, 100)), see the CFrame Wiki page, which lists all the ways of creating a CFrame. Your ConvertToCFrame is strictly slower and completely unnecessary if you know ahead of time that you are dealing with a Vector3 - worse, it has the potential to return 'false', complicating code that might call it. ex, whereas myVal = CFrame.new("hello") will error immediately, myVal = ConvertToCFrame("hello") will not - if you don't use myVal right away, the error may appear much later, making it much harder to figure out what went wrong. (Obviously you wouldn't pass in a string, it's just an example of a situation where your function would return false).

    CootKitty's modification is more efficient, btw, though efficiency shouldn't be the only concern. If you don't like wide lines, you could spread it out:

    local function convertToCFrame(x)
       return typeof(x) == "Vector3" and CFrame.new(x)
          or typeof(x) == "CFrame" and x
          or false
    end
    

    (I still can't imagine a situation where you wouldn't know whether something is a CFrame or a Vector3 and you want the value to become either a CFrame or false.)

    Good math.rad/math.pi tips, though.


  • @chess123mate Oh. Well still, it takes longer to do CFrame.new(Vector3.new(1, 1, 1)) than to do CFrame.new(1, 1, 1). It is like doing Vector3.new(CFrame.new(Vector3.new(1, 1, 1)))). It is not necessary, and wastes time.


  • @hiimgoodpack Good point.

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.