How would I CFrame a part, while still keeping its rotation?

Lets say I have a brick positioned at… CFrame.new(50, 0, -50), and Lets say I want it to point towards Vector3.new(0, 10, 0). This is pretty easy, since the developer-hub has a function I can use to accomplish this:

function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    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

part.CFrame = lookAt(Vector3.new(0, 10, 0), part.Position)

But now… what if I wanted to set the CFrame of the part to be equal to… CFrame.new(25, 0, -25), when I do this… It will no longer be facing Vector3.new(0, 10, 0). Is there any way I could move the part to CFrame.new(25, 0, -25) while still keeping it’s rotation? I have been stuck on this for like 1 or 2 hours, sooo… It’d be nice if you could stop by, and help me.

1 Like

Yep, it’s as simple as subtracting the CFrame from its position to get just the “rotation”. So for example:

local CF =  lookAt(Vector3.new(0, 10, 0), part.Position)
local rotation = (CF - CF.Position)

Then you multiply that by whatever cframe to “rotate it”:

local RotatedCFrame = CFrame.new(5,10,5) * rotation

it should also be noted that CFrame.new(5,10,5) * rotation isn’t the same as rotation * CFrame.new(5,10,5)

2 Likes

For some reason, its not pointing towards Vector3.new(0, 0, 0) when I use this code:

local part = workspace.Part

function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    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

local CF =  lookAt(Vector3.new(0, 0, 0), part.Position)
local rotation = (CF - CF.Position)

part.CFrame = CFrame.new(25, 10, 25) * rotation

It still gets positioned at CFrame.new(25, 10, 25) but its not rotated directly at Vector3.new(0, 0, 0).

1 Like

It should be keeping its initial rotation but I’m guessing that’s not exactly what you want, I’m guessing you want it it to look at the position while moving or no matter what the parts position is, which means the rotation needs to be changed. In that case you should be able to plug the position right into your look at function as the eye, if I understand correctly


“look at” can also be defined as CFrame.new(pos, lookAtPos) btw

1 Like

You know you can just do

part.CFrame = CFrame.new(Vector3.new(25, 10, -25), Vector3.new(0, 10, 0))

as the first parameter represents a position and the second one where should an object look at.
Or am I missing something?

1 Like

that’s deprecated. See CFrame | Documentation - Roblox Creator Hub.

1 Like

It is kind of, yet it really shouldn’t be, it really isn’t too different from using a custom look at function

This post and the thread it’s in talks more about its “depreciation”:

1 Like

@Jaycbee05
I managed to figured out how to do this:

function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    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 
local i = 0

game:GetService("RunService").Stepped:Connect(function()
	i = i + (1 / 25)
	game.Workspace.Part.CFrame = CFrame.new(1, math.sin(i) * 10, 1)
	game.Workspace.Part.CFrame = lookAt(Vector3.new(0, 0, 0), workspace.Part.Position)
end)

Thanks for your help though :slight_smile: !

1 Like