I have been reading the Unity Netcode manual and other related articles, which explains very clearly why it is essential to care about latency. Still, I haven't seen good examples of implementing a solution. Below is a quick video of a bare-bones project simply reproducing the issue. I am using a local host/client model, with a simulated 70ms of delay, and you can see on the client side the bullets always spawn outside the player, which I understand why, but how do I go about fixing it. I just can't find good examples dealing with the issue.
Video here: https://youtu.be/ZuX2f2s3w3o
Here is the code that spawns the bullets:
using Unity.Netcode;
using UnityEngine;
public class PlayerAttacks : NetworkBehaviour
{
public GameObject projectile;
public float FireRate;
float _nextShot;
void Update()
{
if (!IsOwner) return;
if (Input.GetKey(KeyCode.Space))
{
if (_nextShot <= Time.time)
{
if (IsServer)
{
SpawnProjectile();
}
else
{
SpawnServerRpc();
}
_nextShot = 1 / FireRate + Time.time;
}
}
}
[ServerRpc]
void SpawnServerRpc()
{
SpawnProjectile();
}
void SpawnProjectile()
{
var p = Instantiate(projectile, transform.position, transform.rotation);
p.GetComponent<NetworkObject>().Spawn();
Destroy(p, 4f);
}
}