Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to Synchronize Bullets and other Game Objects across Clients?

Discussion in 'Netcode for GameObjects' started by Graphman31, Jul 29, 2023.

  1. Graphman31

    Graphman31

    Joined:
    Dec 6, 2021
    Posts:
    1
    I am working on a Top-Down 2D Multiplayer Shooter, and so far I have been able to use Netcode for Game Objects to get a Host / Server running and multiple clients connected. The problem I am encountering is with getting bullets synchronized across the clients and server. Below is the code on the Player Object that does the shooting.

    Code (CSharp):
    1. public class Shooting : NetworkBehaviour
    2. {
    3.     public Transform firePoint;
    4.     public GameObject bulletPrefab;
    5.     public float bulletForce = 5f;
    6.  
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         if (!IsOwner) return;
    11.  
    12.         if(Input.GetMouseButtonDown(0))
    13.         {
    14.             ShootServerRpc();
    15.         }
    16.     }
    17.  
    18.     [ServerRpc]
    19.     private void ShootServerRpc()
    20.     {
    21.         GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    22.         bullet.GetComponent<NetworkObject>().Spawn(true);
    23.         bullet.GetComponent<Rigidbody2D>().AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    24.     }
    25. }
    And here is the script that is on the bullet:

    Code (CSharp):
    1. public class Bullet : NetworkBehaviour
    2. {
    3.     public GameObject hitEffect;
    4.  
    5.     private void OnCollisionEnter2D(Collision2D collision)
    6.     {
    7.         if (collision.gameObject.tag == "player")
    8.         {
    9.             Debug.Log("Hit Player!");
    10.             collision.gameObject.GetComponent<TopDownMovement>().TakeDamageServerRpc();
    11.         }
    12.         BulletHitServerRpc();
    13.     }
    14.  
    15.     [ServerRpc]
    16.     private void BulletHitServerRpc()
    17.     {
    18.         GameObject effect = Instantiate(hitEffect, transform.position, transform.rotation);
    19.         Destroy(effect, 1f);
    20.         //GetComponent<NetworkObject>().Despawn(gameObject);
    21.         Destroy(gameObject);
    22.     }
    23. }
    It's worth noting that I have a movement script on the player that makes the firepoint always point towards the mouse, so whenever the Shooting Server RPC is called, the force is indeed added in the correct direction and the bullet flies forward. The issue is this: The bullet appears on the clients later than it does on the server / host, and it lags behind. Then, when the bullet hits an object or wall on the server, it despawns, causing the bullet to just vanish mid-shot on the clients. The bullets are being moved via a force applied to their RigidBody2Ds, and they report their position to the server with the "Client Network Transform" scripts, since I am not worried about client/server authority or players cheating. Lastly I have also tried shooting bullets with and without the "Network RigidBody2D" script to the same effect on both.

    Is there a way to sync up bullets so the game is playable for clients? Thanks in advance for all the help!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,015
    Bullets, or any fast moving object for that matter, are typically not synchronized over the network. It depends on the speed of the objects for sure. A missile that's maybe twice as fast as a player should probably be synchronized, but bullets that travel ten times faster should not be synchronized. More so if we are talking actual gunfire - those are typically implemented by raycasting because their velocity can be considered "infinite" in terms of game speed except for extremely realistic sim shooters.

    The way to go about this, assuming a client starts firing fast bullets:
    • client tells server that it starts firing (server relays that to other clients)
    • client locally spawns bullets (not synchronized)
    • other clients also locally spawn high-speed bullets coming from the shooting client given the info they got from the server - the shooting commences until the server tells clients that the shooting client has stopped shooting
    • server knows client's weapon, firing rate and all that and calculates any impacts and sends that info to clients
    Sometimes bullets may miss but hit, or visually hit but actually do not inflict damage. You've seen that in other games - this is to be expected unless the server simulation can do the fancy stuff NGO currently doesn't yet do, mainly server-side rollback (calculating the state of the simulation back at the time when the client started shooting, not when the message was received by the server).