've created a basic MMO system using Unity3d and a custom c# server. Right now, players can walk and attack eachother and it works great!
Now, I'm implementing the mobs system and I'm not sure how to do it, here's my thoughts!
Spawning
-
The server spawns the mobs on the world based on a % (This works great)
-
Then, players can see the mob fixed on the position where the server spawned it.
Moving mobs
-
I'm not sure how to do this, I've thought about 2 options
A) The server changes the position of each mob every X seconds (say 3-4) and sends the new position to all the players around, then each player starts moving the mob towards that position. I know this would cause that different players sometimes see the mob on different locations...
B) Every 3 or 4 seconds (same) the server sets a new final position for the mob, and every 0.5s (server sends sync snapshots every 0.5s), sends the mob position to all players around. This seems a better solution but there's gonna be so many packets travelling around...
Mobs Attack Players
-
Using the option B) for moving mobs around, the server could detect when one player P is close enough to the mob M, so M should start attacking P.
-
But here's the big question the server doesn't know ANYTHING about the terrain so, now M have to go towards P to 'kill him', but the server can't do the pathfinding algorithm to move M to P's position... So I came with an idea!
Idea!
-
When the server sets M to attack P, then P gets the control of M so the player starts controlling the mob and makes it go towards him using the pathfinding algorithms of unity! Then, the player every 0.5s is going to send his position and M's position to the server, and the server updates M's position exactly where P said it has to be.
-
When the server detects that M is close enough to P to attack, M causes damage to P even if he's still running away and M is going after him.
What do you think? Would it work? Any better solution? Thanks :)