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.
public class Shooting : NetworkBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 5f;
// Update is called once per frame
void Update()
{
if (!IsOwner) return;
if(Input.GetMouseButtonDown(0))
{
ShootServerRpc();
}
}
[ServerRpc]
private void ShootServerRpc()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
bullet.GetComponent<NetworkObject>().Spawn(true);
bullet.GetComponent<Rigidbody2D>().AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}
And here is the script that is on the bullet:
public class Bullet : NetworkBehaviour
{
public GameObject hitEffect;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "player")
{
Debug.Log("Hit Player!");
collision.gameObject.GetComponent<TopDownMovement>().TakeDamageServerRpc();
}
BulletHitServerRpc();
}
[ServerRpc]
private void BulletHitServerRpc()
{
GameObject effect = Instantiate(hitEffect, transform.position, transform.rotation);
Destroy(effect, 1f);
//GetComponent<NetworkObject>().Despawn(gameObject);
Destroy(gameObject);
}
}
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!