Making part look away from position

Alright, so my problem is pretty basic. I want to make a part look away from a certain position using CFrame.lookAt on one line but I’ve forgotten how to and I can’t find any material covering it. So far the code I’m using is below.

local OriginalPosition = Part2.Position
Part2.CFrame = CFrame.lookAt(Part1.Position,Part2.Position)
Part2.Position = OriginalPosition

As you can see, that’s three lines of code and also setting the properties of the part twice instead of once. I’m pretty sure I remember being able to create a CFrame that looked away from a position instead of towards it without having to reset the position after setting the CFrame. If there is a way please let me know!

You could just rotate it 180 degrees:

Part2.CFrame = CFrame.lookAt(Part2.Position, Part1.Position) * CFrame.Angles(0, math.pi, 0)

Or you could build the matrix yourself (didn’t test this):

local back = Part1.Position - Part2.Position
local right = Vector3.new(0, -1, 0):Cross(back)
local up = back:Cross(right)
Part2.CFrame = CFrame.fromMatrix(Part2.Position, right, up, back)
1 Like
local OriginalPosition = Part2.Position

Part2.CFrame = CFrame.new(Part1.Position,Part2.Position)
Part2.Position = OriginalPosition

Try this

The documentation of CFrame.lookAt says:

This function replaces the CFrame.new(Vector3, Vector3) constructor (see above) which accomplished a similar task. This function allows you to specify the up Vector, using the same default as the old constructor.

So they are the same :slight_smile:

kk

local OriginalPosition = Part2.Position

Part2.CFrame = CFrame.new(Part1.Position,Part2.Position)
Part2.CFrame = Part2.CFrame * CFrame.Angles(0, math.rad(180), 0 )
Part2.Position = OriginalPosition

Idk if this will work just try

If it don’t Idk what to do lol

Alright, so my question has already been answered, but I want to correct you that they are not the same. CFrame.lookAt is a new function that should be used in new works and is meant as a better replacement for CFrame.new with an additional parameter. The additional parameter for CFrame.new only remains for backwards compatability.

1 Like

You are literally reusing the code that I already supplied. Plus it isn’t relevant to what I was asking.

You’re right, I just meant that

CFrame.new(a, b) == CFrame.lookAt(a, b)

for angles <90 degrees.