Hello. I try to create online top-down shooter. Character rotation is controlled by mouse. So on the server there is no problems, but on client bullet spawning is delayed. When I turn my character and shoot, bullet spawns on the position where characters gun was few moments ago, but on the server everything is fine. Also I add force to the bullet after spawning.
GameObject instance = Instantiate(bullet, playersGun.position, playersGun.rotation) as GameObject;
instance.GetComponent<Rigidbody2D>().AddForce(playersGun.up * power);
That is shooting script.
On the screenshot the lower bullet is servers and upper bullet is clients one. How can I sync it and avoid delay? Maybe someone can help me? Thank you.
I am not good in networking but i thing you should send only coordinates for bullet spawn to the client. Client shoud instantiate it. Here you send whole bullet isntance. Next you should think about object pooling because instantiate objects is havy solution.
It depends on what you're planning on doing. If you want the bullet to spawn at the gun, you could do that, but then the trajectory would be different from the server. The client might hit someone, but the server didn't and ignores it, which might upset the player. Keeping both in sync however would result in the player seeing his character in a totally different state all the time. Of course he wants his character to react to his inputs the second he pushes a button. Unfortunately, it's not possible to get everything at the same time, because there's the delay of network traffic. The best choice would be to predict the enemies position at the client side to compensate for the delay. This won't be accurate either, but that's the problem with networking. If I may suggest reading these articles, they give a great understanding of fast paced network games and how they compensate the problems that arise:
As an additional note: Street Fighter renders several frames beforehand to accommodate for changes due to network lag. But I don't know where to read about it
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
110 People are following this question.
Network client : cannot fire
server and client sync
UNET Network.Spawn and child objects
Synchronizing Gameobjects over a Network Server < - > Client Sync
UnityScript not recognizing NetworkServer "Unknown Identifier"