I’ve been trying to get my bullets to sync across my Netcode for game objects server using RPCs (since the bullets aren’t the server). No matter what I do it will not run, debugging and profile analysis shows that RPC bytes are being sent but not received. Any help is appreciated and thanks in advance
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine.Networking;
using UnityEngine;
using System;
public class Bullet : NetworkBehaviour
{
public float speed = 10f; // Speed of the bullet
public float damage = 10f; // Damage inflicted by the bullet
public float lifetime = 2f; // Time in seconds before the bullet is destroyed
private void Start()
{
Debug.Log("Called");
NetworkCallSpawn();
}
private void NetworkCallSpawn()
{
Debug.Log("2ndCall");
NetworkSpawnServerRpc();
}
[ServerRpc(RequireOwnership = false)]
public void NetworkSpawnServerRpc(ServerRpcParams rpcParams = default)
{
Debug.Log("Success");
Debug.Log("BulletCallSERVER");
this.GetComponent<NetworkObject>().Spawn();
}
public void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("BulletImpact");
// Check if the bullet has collided with a player
PlayerMovement player = other.GetComponent<PlayerMovement>();
if (player != null)
{
DestroyBulletServerRpc();
// Inflict damage to the player and destroy the bullet
player.TakeDamage(damage);
Debug.Log("Hit");
}
if (other.GetComponent<BulletDeflector>() == null)
{
DestroyBulletServerRpc();
Debug.Log("Wall");
}
else
{
Debug.Log("Deflect");
return;
}
}
[ServerRpc(RequireOwnership = false)]
public void DestroyBulletServerRpc(ServerRpcParams rpcParams = default)
{
Debug.Log("Destroyed");
GameObject bulletthis = this.gameObject;
bulletthis.GetComponent<NetworkObject>().Despawn();
Destroy(gameObject);
}
}
Above is bullet script and below is shooting script
using System.Globalization;
using UnityEngine;
using UnityEngine.InputSystem;
using Unity.Netcode;
using System.Collections.Generic;
public class Shooting : NetworkBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
[SerializeField]private List<GameObject> bullets = new List<GameObject>();
public float bulletForce = 20f;
private ulong clientId;
private void Start()
{
clientId = bulletPrefab.GetComponent<NetworkObject>().OwnerClientId;
}
public void OnShoot()
{
FireBulletServerRpc();
}
[ServerRpc(RequireOwnership = false)]
public void FireBulletServerRpc(ServerRpcParams rpcParams = default)
{
Debug.Log("Shoot" + this.GetComponent<NetworkObject>().OwnerClientId);
if (!IsOwner) return;
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
bullets.Add(bullet);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}