Is there a way to select which surface looks at the target with CFrame.lookAt()?

So, i created a Sans Gaster Blaster and Bone Throw tool, the problem is that i don’t know how to choose what surface looks at the target.

1 Like

The surface chosen is always the front surface. If you need to change this, you can apply a simple rotation immediately afterwards (depending on which face you need):

part.CFrame = part.CFrame * CFrame.Angles(0,math.rad(90),0)

CFrame.Angles(x,y,z) can be read about in detail here, but in short, many CFrame constructors such as CFrame.Angles uses radians. This means when you enter a number such as 90 degrees, you need to convert it to radians using math.rad(90).

If this exact code doesn’t work (it likely will rotate the part incorrectly), then you can try rotating by a different amount such as 180 or -90 degrees, or perhaps applying the rotation to a different axis. In the example, applied it to the Y axis since that’s most often the axis you’d want to use for a situation like this.

1 Like

Hello, while i’m learning about this could you tell me what’s the Angle for the Right Surface?

I actually don’t remember off the top of my head because in a situation like this I would test it myself in just a few seconds to find out. It’d definitely either be 90 degrees or -90 degrees, but I’m not sure which.

1 Like

I tried with many deegres and the surface doesn’t change.

1 Like

You can set the cframe to look at the target normally for front surface, or rotate the angles afterwards for other surfaces, below’s an example for all the surfaces,

local part = INSERTPARTHERE
local currentPosition = part.Position
local lookAtPosition = INSERTLOOKATPOSITIONHERE

local frontSurface = CFrame.new(currentPosition,lookAtPosition)
local backSurface = CFrame.new(currentPosition,lookAtPosition) * CFrame.Angles(0,math.rad(180),0)
local rightSurface = CFrame.new(currentPosition,lookAtPosition) * CFrame.Angles(0,math.rad(90),0)
local leftSurface = CFrame.new(currentPosition,lookAtPosition) * CFrame.Angles(0,math.rad(-90),0)
local topSurface = CFrame.new(currentPosition,lookAtPosition) * CFrame.Angles(math.rad(-90),0,0)
local bottomSurface = CFrame.new(currentPosition,lookAtPosition) * CFrame.Angles(math.rad(90),0,0)
9 Likes

It works, thank y’all so much. : D

1 Like