Accidentally made server projectile variant of client

(Sorry, it seems like I post here a lot.)

In a lesson ahead or two, I got the tanks to fire, but for some reason, my host can see all my client’s projectiles, but not the other way around.

Doing some digging, I found that I accidentally made my ServerProjectile a variant of my ClientProjectile on accident. Is there any way to change this?

Capture

Delete the server projectile and create it again, using the correct base. The server projectile isn’t that big, so it will take 3 minutes to do. There is no way to change the base

2 Likes

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);
    }


}

The condition here is wrong. You want to create the projectiles if it’s not the owner (so, exit if it is). It should be

if (IsOwner)
    return;
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms