How do I change the Z coordinate using CFrame.LookAt()?

Hi, I am trying to find out how I can change the Z coordinate of an NPC using CFrame.LookAt(). So what I mean is that I only want the Z coordinate changing and I need to use CFrame.LookAt() for that. But my problem is that all the CFrame coordinates are changing but I only need one of them changing. Does anybody know how I can do that with this single line of code?

game.Workspace.Boss1.HumanoidRootPart.CFrame = CFrame.lookAt(game.Workspace.Boss1.HumanoidRootPart.Position, PlayerFound.Character:FindFirstChild("HumanoidRootPart").Position)

I tried doing that but it always says that it cant convert it into Z

1 Like

Do you want the CFrame to move in the direction Boss1 is facing, presumably along Boss1’s Z axis, or do you want it to move on the Z axis for the entire world?

I want the NPC called Boss1 to face at a player that is being choosen by this line of code:

local PlayerFound = Players:GetChildren()[math.random(1, #Players:GetChildren())]

Then I am getting the character of the choosen player and then the HumanoidRootPart.
The boss is supposed to face to the players HumanoidRootPart but my problem is that if the player gets too close or tries to walk under the NPC the NPC falls over because its facing down to the player. I am using a while task.wait(0.05) command to make the CFrame change all the time. Thats why I only want the Z CFrame coordinate to change. I guess I need the Z axis from the NPC so it wont fall over or look down.

Example with simple parts:

local part1 = workspace.Part1

local part2 = script.Parent

local lookat = CFrame.lookAt(part1.Position,part2.Position)

local x,y,z = lookat:ToOrientation()

script.Parent.CFrame*=CFrame.Angles(0,y,0)

That’s how it probably looks with boss:

local plrpos=PlayerFound.Character:FindFirstChild("HumanoidRootPart").Position

local prim=game.Workspace.Boss1.HumanoidRootPart

local primPosition=prim.Position

local lookat = CFrame.lookAt(primPosition,plrpos)

local x,y,z = lookat:ToOrientation()

prim.CFrame*=CFrame.Angles(0,y,0)

Also, I don’t recommend writing these long lines. They probably look cool for you, but in reality it’s bad for your code and hard to maintain

1 Like

So you want the boss to turn towards the player but ignore any Y-axis difference? In that case subtract out the Y difference before using LookAt:

local bossHrp = game.Workspace.Boss1.HumanoidRootPart
local playerPosition = PlayerFound.Character:FindFirstChild("HumanoidRootPart").Position
local yDifference  = bossHrp.Position.Y - playerPosition.Y
bossHrp.CFrame = CFrame.lookAt(bossHrp.Position - Vector3.yAxis * yDifference, playerPosition) + Vector3.yAxis * yDifference

Note that the difference is added back at the end so the boss doesnt get pushed down.

1 Like

Somehow the NPC was going crazy after I inserted the code into my script. It began to spin very fast all the time. But thanks for the help anyways

only the Y position of the player needs to be corrected

local b = game.Workspace.Boss1.HumanoidRootPart.Position
local p = PlayerFound.Character:FindFirstChild("HumanoidRootPart").Position
game.Workspace.Boss1.HumanoidRootPart.CFrame = CFrame.lookAt(b, Vector3.new(p.X,b.Y,p.Z))

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.