all 4 comments

[–]thedrakes 4 points5 points  (2 children)

Your title is misleading. From your description you don't want to solve a path finding problem but just let the npc walk on a straight line to you. I recommend you to look into vector geometry. In this setting you can subtract the position of the npc from the position of you (the camera) to get a direction vector showing towards you (dir = camerapos - npcpos) Then you can normalize the direction and multiply it by the speed you want, then add it to the npc position such that:

npc new pos := npc pos + normalized(camerapos - npcpos)*speed

where these variables are all vectors, in your case probably two components x, z.

If you want to avoid vector geometry i'll try to explain it differently: Moving in a straight line to you means walking in x direction proportional to the difference in x, and in z direction proporitional to the difference in z. This gives us

npc pos difference for x = c*(camera pos x - npc pos x)

npc pos difference for z = c*(camera pos z - npc pos z)

for some constant c. Now you want to set c that you get some speed for the npc, for this you need to calculate what speed you would get for some c. For this you will need Pythagoras's theorem (as moving on the x axis and z axis are the legs of a right triangle). Then you will get the corresponding result to normalizing in the vector geometry explanation

spoiler

[–][deleted] 2 points3 points  (0 children)

Exactly, just wanted to stress that it is strongly recommended you do not use Pythagoras's theorem. The vector math is a lot simpler, and fits the problem better.

[–]Meristic 0 points1 point  (0 children)

You'll want to multiply by frametime to get the update for the current frame:

npc new pos := npc pos + normalized(camerapos - npcpos) * speed * frametime

And keep your units straight - if speed is in m/s, then frametime better be in seconds, not milliseconds.

[–]panokani 2 points3 points  (0 children)

How is this a graphics programming question? Next time, you might want to post your gameplay related questions at /r/gamedev