# Best way for randomized, but controlled NPC movement?

## Recommended Posts

Many top-down (role playing) games live from NPC movement to deliver some vividness to the player.

But how to properly set this up?

My basic idea was:

- if npc is not moving, let roll some dice with a random number generator and a very little humble chance to win (<0.1%) each update

- if the random number is chosen, generate a random number for x and y in respect of it's avaible range and set this as a target to move to and set him to move.

- in each update move toward the target

- if the distance between the npc and the target undergoes a certain threshold or npc collides, set him to non moving.

This might work some how, but has the effect that they will go wild. But some NPC with special abilities (merchants etc), must be found with a specific range. I could just negate the cords, after he finishes moving, but this looks very unnatural, as the NPC starts moving in star-formation.

Anyone has an idea how to solve this? And has anyone an idea how the algorithm choosed can be optimized?

##### Share on other sites

How about you create 'paths' that they can walk. Then use your random number to decide which path to take.

So for your merchant NPC you could have a path that goes to the well and back. Then there's a path where they walk around their stand (or crates) as if they're checking for damage.

This way it's still random movement, but with context.

##### Share on other sites

I suggest that you consider using a finite state machine to define the behavior of your NPCs. The author of the book Programming Game AI By Example has made a tutorial about finite state machines available online. Quoting from the page: "The NPCs (non-player characters) in RTSs (real-time strategy games) such as Warcraft make use of finite state machines. They have states such as MoveToPosition, Patrol, and FollowPath."

##### Share on other sites

Even I way aiming for a more generic solution, I will set up a finite state machine and give every npc customized behaviour, dialogs and paths.

Thanks.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.