Incorrect example under the CFrame.fromMatrix() function

The CFrame.fromMatrix() constructor has an incorrect example under it of how to use the cross product of the calculated vectors to create a new CFrame, here is the link:

Here is the code:

function lookAt(target, eye)
    local forwardVector = (eye - target).Unit --[[
    Here is the issue it should be (target-eye).unit
    this current vector is the - look vector, hence later on
    the -right vector is calculated.
    ]]--
    local upVector = Vector3.new(0, 1, 0)
    -- You have to remember the right hand rule or google search to get this right
    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)
 
    return CFrame.fromMatrix(eye, rightVector, upVector2)
end

This error causes the back surface to actually face the target and not the front surface.

Here is the correct code:

function lookAt(target, eye)
    local forwardVector = (target - eye).Unit -- This line has been changed
    local upVector = Vector3.new(0, 1, 0)
    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)
 
    return CFrame.fromMatrix(eye, rightVector, upVector2)
end
2 Likes

Hi, thank you for the correction! I’ve updated the sample.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.