So I’ve been using this code for a while to lock players and npcs to the Z axis
--Lock the Z axis by updating it to be the same as the MiddlePoint Z axis
if HRP.CFrame.Z ~= MiddlePoint.CFrame.Z then
CFrame.new(HRP.CFrame.X, HRP.CFrame.Y, MiddlePoint.CFrame.Z)
end
--Face opponent
local FacingDirection = math.floor(Opponent.Character.HumanoidRootPart.CFrame.X - HRP.CFrame.X)
FacingDirection = math.sign(math.sign(FacingDirection) + 0.5)
CurrentFacingDirection = FacingDirection
local NewPos = HRP.Position+Vector3.new(FacingDirection,0,0)
HRP.CFrame = CFrame.new(HRP.Position,NewPos)
And I know it probably isn’t the best solution, but it works pretty well
However today I’ve realized that it causes issues when trying to do knockback as you can see from the video below it causes this weird teleporting thing to happen
Here’s how It looks without the code enabled
So I wanted to know how I could still keep the player/npc locked on the Z axis and have them face the opponent without interfering when trying to do knockback
I’ve used AlignOrientation | Roblox Creator Documentation in the past on NPCs. It provides a really simple interface for controlling what they’re looking at. In your case it would also automatically handle the facing direction for you in the scenario that the two characters switched sides (of course you would still need an animation for each side).
I would add an attachment in each character’s root part and add the AlignOrientation into the the NPC’s root part. Set AlignType to Parallel, PrimaryAxisOnly to true, RigidityEnabled to true and Mode to TwoAttachment and set the Attachment0 to the NPC’s attachment in its root part and the Attachment1 to the attachment in the player’s root part attachment.
You can, what I was suggesting was specifically for the NPC but it would work fine in both. AlignOrientation attempts to line up two Attachments, so you only need one AlignOrientation and Two attachments for the NPC. If you add an AlignOrientation to the player character then you just do the opposite with the Attachment0 and Attachment1.