How to make a networked enemy in unity and mirror - unity3d

I'm making a multiplayer game and for kind of all of it I need to have enemies and AI characters.
-for example 5-10 players in a match fighting a monster together. and the monster needs to be synced for all clients.
-or when there are not enough players online the server would add AI bots to the match so the player wouldn't get board.
I tried to make the AI and spawn it and give its authority to one of the players so the AI calculation doesn't cost the server.
Even though I couldn't have the server-side AI because Match Interest Management and that wasn't possible by that.
The problem starts when for example 2 players join a 4-player match and server adds 2 more bots everything works fine until when the match starts the first player which controls the AIs is fine but the other player gets freeze. I look for any infinite loops in my codes but I didn't find anything. I tried delaying spawns but still nothing.
The freezing happens when I use the dedicated server. in the editor and local network there is a very low chance it would happen.
if anyone knows Mirror Please help me if you think you can.
Thank You. Sorry if I didn't explained well.

Related

How can I sync local movement to Photon Network in Unity?

im making a multiplayer FPS game, and I have pretty much all the networking done and working. The issue is, that photon is syncing to players local movement, but if u have a bad connection, you could not tell it is happening cause player send late updates to the server and you can see other players lagging but you can move free and smoothly.
Im aware a lot of games work this way but i also know there are games like medal of honor that your player get stock in the last place every one else saw you. I need to know how to implement this on my game, because gameplay depends on it.
What you describe is "local prediction" of actions / movement.
If I understand this right, you want clients to not act or move if there wasn't a confirmation? If so, you'd need an authoritative host or server. You'd always wait for a confirmation from the host to move the player in any way.
But .. this is not going to work well. Players will feel the lag very much and this is annoying.
You probably want to predict the movement, then correct the local situation if the server disagrees.
I would recommend using Fusion instead of PUN for shooters. It can be run with host and has a nice API to reconcile the actual state from prediction to what the host simulated.

Netcode Behaviour index was out of bounds. Did you mess up the order of your NetworkBehaviours? Unity Netcode for GameObjects

So I've been following some tutorials online and have been trying to create a multiplayer 3D game, and so far, all I want to do is for two clients to be able to connect to a server, spawn their players (spheres), and move their spheres around and into each other, causing collisions.
I have created a player movement script to handle input - just simple WASD and space-jump movement.
To handle the networking, I followed Tarodev's Netcode video to set everything up.
Here is where the problem begins. Using ParrelSync, I run the game on one editor and a clone on another. I then use a GameObject with the NetworkManager script to start a host on one editor, and a client on the other. Upon connection, the client and host both spawn players, but then the problem begins. As the client's player starts falling down, the host's editor starts experiencing errors and warnings. As mentioned in the title, here they are:
[Netcode] Behaviour index was out of bounds. Did you mess up the order of your NetworkBehaviours?
[Netcode] Network variable delta message received for a non-existent behaviour. NetworkObjectId: 2, NetworkBehaviourIndex: 2
I did a bit of digging and found that the error was being thrown by a function in the NetworkObject script, but I couldn't really understand why or how to fix it, hence this post.
Errors are only thrown on the client when the host moves its player, and errors are only thrown on the host when the client moves its player. The transforms of the players are not being transmitted between host and client at all, despite the player prefab having a Client Network Transform script, and a NetworkObject script.
Here is the link to the code if you want to reproduce the error or understand it better: https://github.com/XR101/Sumo101
Any help would be appreciated as this error is driving me insane.
It seems to me like this could be an error in the order of components on your game objects.
If there's only one component on each object, then make sure you're instantiating them correctly on the server via NetworkServer.Spawn
If you're sure you've done those things, then it's probably a component mismatch due to PlayerMovement and PlayerMovement2 being used. I have zero experience with Unity's networking stuff but it seems from the docs that they're intended to communicate with a NetworkBehaviour of the same type.

UMMORG/Mirror Networking - Moving A Connection From World To Lobby

I have been working for some time on a project using the UMMORG framework, which is built on Mirror networking. However, as of recent, I have been having an issue I just cannot solve.
In my project, when a player character is killed, I want the player to be moved from the game world back to the lobby. Or, in more technical terms, for the player's NetworkConnection to me moved from the Network State 'World' to the Network State 'Lobby.'
I have tried many times, but have been unable to find a way to do this. The consistent issue is with the player NetworkConnection not being found as a key in the Lobby dictionary. However, attempts to manually add the player NetworkConnection to the Lobby dictionary have failed. I'm assuming that Mirror requires a specific process to move a connection back to the lobby, but I just can't figure it out!
TLDR: How can I move a NetworkConnection in Network State 'World' to Network State 'Lobby?'
Thank you so much for your help! I will be checking this thread as often as I can, so if I haven't provided enough information, I would be happy to provide more. I have not changed any of the netcode in my UMMORG project, so if you have found a solution for this in your UMMORG or Mirror project, I would love to hear it! Thanks again!
Did you use MatchInterestManager?
I used MatchInterestManager and attach NetworkMatch to my player script.
Whenever I needed to change player state (for example from Lobby to Game or Game to Lobby), I simply change NetworkMatch.matchID from "lobby".toGuid to "gameName".toGuid.
Feel free to leave comments, and I'll do my best to answer.

Sending game state only for entities that are "visible"?

Let's say I have an open world game that supports multiple players.
The game stores their positioning by X and Y in this world.
Also, whoever is playing the game can see a canvas of 11x11, where his player-self is represented on this screen, always centered.
The game world has 1000x1000 squares to walk using keys.
What I know is that:
I could emit events whenever a player walks,
check this event data at the server-side (to see if it is possible and the speed is correct/anti-cheating measures),
update the game state which contains all players and positioning,
re-emit this state from the server so every client would be able to render properly this new player.
The problem is, should I really update someone who isn't even being seen?
When everyone is walking around, moving items, earning levels, etc - those events are being emitted from the client, and that's okay, but thinking about the server-side, it will re-emit that for each update state, and, maybe that will be overloaded?
Also, sending the whole game state, even if it isn't being rendered, opens so many breaches to cheating that this made me think that there is another option.
I'm a beginner at Networking and Game Development, and that is being hard to get into my mind - so I decided to try and put it into a question. This way, maybe with someone reading what I'm thinking about, I might get some clarification. Perhaps I'm just thinking about it the wrong way.
Q: should I really update someone who isn't even being seen?
There is no need.
The normal way MMORPG games do in the server side is to cyclely process network packages and some other calculations like the connection of players like skill cast or something else.
The central of a server may look like below
void run()
{
while (true)
{
processNetwork();
processSkills();
processMoves();
...
}
}
The loop will run several times a second, like 20 frames a second is enough cause players can not feel the little frame, they think they move/play smoothly but the trueth is not.
For your question, player only need to see some little area, when he moves, the server will braodcast his postion to others in the area and the players in his area in the next frame.
And that is just the simpliest model, actual model will be more complicated and we will detach different functions of the game to different server, sucn as chat server, battle server, auction server and others.

How to synchronize a 1v1 realtime action online mini game?

What I'm trying to make
Hi, game development newbie here. The game I am trying to make is fairly simple. It's almost exactly like the old FC game "Ballon Fight" except that I'm trying to make it online where players can go through a match making to find opponents.
BalloonFight:
What I Read
I have read some articles, and found most of them lead to two approaches:
Put all game logic on the client, and the client sends player inputs to server on every frame update. The server acts like a dispatcher which only makes sure player A's input is received by both client A and B. My understanding is that if we see the client in this case as a pure function, and if the two players' inputs are received by each other, the game should produce same results on both clients. Thus synchronization is achieved.
Put all game logic on the server, and let the server do the calculations and send back results to both clients. In this case, clients only worry about displaying.
My Fears
Solution 1 sounded like a simpler one to me, but immediately I realized when network problem is put into account, it becomes incredibly complicated. Losing player A's connection for a few seconds means all the input is lost in that period. What I can guess is, to counter that, the server has to detect whether player A is lagged out and accumulate input from player B until player A is back then feed all the accumulated input to player A's client. Player A's client then need to do a fast forward to catch up. This sounds like there's huge amount of infra work on both client side and server side.
Solution 2 on the other hand looks very daunting to me, since for now I have only written some games on the client side.
My questions
in order to make a simple online game like this, what is the most beginner friendly way to synchronize game state?
if I were to use solution 1 stated above, is there any framework that provides such infra so that I don't have to handle network issues all by myself?
In advance, thank you game dev gurus.