- Home /
How to avoid spawn delay on client?
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.
[Command]
void CmdShoot()
{
GameObject instance = Instantiate(bullet, playersGun.position, playersGun.rotation) as GameObject;
instance.GetComponent<Rigidbody2D>().AddForce(playersGun.up * power);
NetworkServer.Spawn(instance);
}
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. $$anonymous$$eeping 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:
http://www.gabrielgambetta.com/fpm1.html
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