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.
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).
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
@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)