Okay, I fixed that, but now neither screen shows the other player’s bullets. I went back through the lesson and followed it to a T, checked the GitHub page, and it still won’t work. If you could, would you mind taking a quick read through my code to see if there’s anything wrong on its end?
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class ProjectileLaunch : NetworkBehaviour
{
[Header("References")]
[SerializeField] InputReader inputReader;
[SerializeField] Transform projectileSpawnPoint;
[SerializeField] GameObject serverProjectilePrefab;
[SerializeField] GameObject clientProjectilePrefab;
[Header("Settings")]
[SerializeField] float projectileSpeed;
bool shouldFire;
public override void OnNetworkSpawn()
{
if (!IsOwner)
return;
inputReader.PrimaryFireEvent += HandlePrimaryFire;
}
public override void OnNetworkDespawn()
{
if (!IsOwner)
return;
inputReader.PrimaryFireEvent -= HandlePrimaryFire;
}
void Update()
{
if (!IsOwner)
return;
if (!shouldFire)
return;
PrimaryFireServerRpc(projectileSpawnPoint.position, projectileSpawnPoint.up);
SpawnDummyProjectile(projectileSpawnPoint.position, projectileSpawnPoint.up);
}
void HandlePrimaryFire(bool shouldFire)
{
this.shouldFire = shouldFire;
}
void SpawnDummyProjectile(Vector3 spawnPos, Vector3 direction)
{
GameObject projectileInstance = Instantiate(clientProjectilePrefab, spawnPos, Quaternion.identity);
projectileInstance.transform.up = direction;
}
[ServerRpc]
void PrimaryFireServerRpc(Vector3 spawnPos, Vector3 direction)
{
GameObject projectileInstance = Instantiate(serverProjectilePrefab, spawnPos, Quaternion.identity);
projectileInstance.transform.up = direction;
SpawnDummyPorjectileClientRpc(spawnPos, direction);
}
[ClientRpc]
void SpawnDummyPorjectileClientRpc(Vector3 spawnPos, Vector3 direction)
{
if (!IsOwner)
return;
SpawnDummyProjectile(spawnPos, direction);
}
}