Correct, server side can fire and shown on both sides server and client. Client can not instantiate anything even a debug.log.
I have modified it a bit since and am basing it only on a click input instead of a target. Basically 90% same code w/o the targeter, but here is the unit firing code.
using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.ProBuilder;
public class UnitFiring : NetworkBehaviour
{
[SerializeField] private GameObject projectilePrefab = null;
[SerializeField] private Transform projectileSpawnPoint = null;
[SerializeField] private float fireRate = 1f;
private Camera mainCamera;
private float lastFireTime;
[ServerCallback]
private void Update()
{
if (!hasAuthority) { return; }
mainCamera = Camera.main;
if (Mouse.current.leftButton.isPressed)
{
if (!CanFireAtTarget()) { return; }
if (Time.time > (1 / fireRate) + lastFireTime)
{
Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue()); //gets mouse in current location of screen
if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity)) { return; } ; //sends out ray from mouse click to see if it hit anything
Quaternion projectileRotation = Quaternion.LookRotation(hit.point - projectileSpawnPoint.position);// From target is, minus our position
GameObject projectileInstance = Instantiate(projectilePrefab, projectileSpawnPoint.position, projectileRotation);
//we can now fire
Debug.Log("Fire!!!");
NetworkServer.Spawn(projectileInstance, connectionToClient);
lastFireTime = Time.time;
}
}
}
[Server]
private bool CanFireAtTarget()
{
return true;
}
}
Thanks.