Log in to vote

How to make CFrame.LookAt() only look through with one axis?

Asked by 1 month ago
Edited 1 month ago

As in the question how would you make it so LookAt will only rotate through one axis instead of all of them? I tried just putting .Y at the end but errored and said "CFrame expected got number", I also tried to put the look at in part of a vector3 like this,LookAt,0) but since it was part of a loop it would just start spinning which is not what I wanted.

NPCCharacter.PrimaryPart.CFrame  = CFrame.lookAt(NPCCharacter.HumanoidRootPart.Position, Character.HumanoidRootPart.Position).Y

1 answer

Log in to vote
Answered by 1 month ago

You can change the Y component of the target position to be the same as the NPC's Y position to have it only take X and Z into account. Modifying your code would give the following:

local npcPosition = NPCCharacter.HumanoidRootPart.Position
local targetPosition = Character.HumanoidRootPart.Position

NPCCharacter.PrimaryPart.CFrame = CFrame.lookAt(
    npcPosition,, npcPosition.Y, targetPosition.Z)
tysm Rayguyban2 76 — 1mo

Answer this question