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