An idea I had myself, was to (imagining) a rotation of the coordinate system around the y-axis, so that the same code I gave could work with now x = z and y = y. However, that doesn't seem to work when the NPC has any other rotation than 0.0 degrees.
Always keep in mind that a lot of people are active on this forum in their spare time.
They are sacrificing time they could easily spend on things they would rather do, to help you instead.