- Home /
NPC's bump into eachother trying to reach a position
So I have a player gameobject and 8 NPC gameobjects. Each of these objects have an empty child object called "TalkingPosition", which is dragged a bit in front of the character. Then at "random" points characters (NPCs or the player) walk to eachothers TalkingPositions. Walking is done using a Navmesh with Navmeshagents. (the player is also a navmeshagent)
Now a problem occurs when for example 2 or 3 NPC's walk to let's say the players TalkingPosition. (NOTE that they do not walk concurrent, they walk one after the other. So if one is done walking, the next one starts). So when this happens, one NPC is still on that spot and the other NPC can not reach it (immediately), he 'awkwardly' pushes the other NPC away to try and reach the destination. Sometimes this works, but sometimes he does not reach the destination, and thus the game is stuck waiting endlessly for that one to arrive.
I have 3 different scenes, an outdoor park, an office and a house. In the park a solution could be to move the NPC a small bit from the TalkingPosition, but in the office and the House is not that much room to do this 'safely'.
Can anyone suggest a solution for this matter?
Answer by Nimred · Jan 08, 2015 at 03:59 PM
If NPC2 walks to the Player with the intent of talking to him, but NPC1 is already there, there's several things you can do:
Just like in real life, NPC2 could join the conversation with Player and NPC1. Which means you can no longer just use a single TalkingPosition, you need several, most likely in a circle to mirror what people do.
Another possibility is to detect NPC2 is coming to talk to the Player, and end the conversation with NPC1 earlier than planned. That could happen in real life if NPC1 does not like NPC2 :)
More hacky, in case you don't need too much detail for your game: you could always loosen the restriction to reach the TalkingPosition. NPC2 can be considered to have arrived at the TalkingPosition if he is right on it OR (is going to it && is less than x meters from it && has collided with another agent). That would look silly but your game would keep running :) That's what you get with hacks.
Also, you have a restriction in your game that only one NPC can move at a time, most likely to avoid doing path avoidance between them. It would make sense to also add a restriction where an NPC cannot even start walking to an agent if that agent is already talking to someone. If each agent is a "Talking resource", then deciding to go talk to one of them locks that resource, until it is unlocked when the conversation ends. That is just like real life too: your friend was talking to someone, you notice the conversation ends and start heading to your friend.
Hope that helps.
Thank you for the reply. I personally like the 1st approach you gave.
Do you have any idea how to implement something like this? I would then want the NPC2 to choose the circle which is in front of the player (if its free) and otherwise the one closest to him?
You could start by setting up more TalkingPositions as child objects of the agent, and space them the way you like. You will have to decide on a maximum number of them, and if all become occupied (if that can happen in your game), you will still have to use the 2nd or 4th approach as a fallback.
When it's time for NPC2 to move, you need to find out which position to go to. If you had a "TalkingPosition$$anonymous$$anager" script on your agents that contains an array of their TalkingPositions, then you'd also be able to order them the way you like, so that the first in the array is the one NPC1 chooses, NPC2 takes the second, etc.. Then you need to keep track of which ones are in use, with a Dictionary maybe; this way when NPC2 has to move, you can loop through the list and return the first available one. Or take the distance into account and return the closest available one.
You could also generate the talking positions by script, to handle more agents, and make things more organic, but it's a bit more complicated.