Hey everyone I need a help.
I am making a multiplayer fps game by following a youtube tutorial the tutorial is using unet for networking which is deprecated now, I am implementing the taught concepts using netcode in unity
The problem is that in the function ShootServerRpc() in PlayerShoot.cs the client is returning from the if(!IsLocalPlayer) statement but it is working fine for the Host
PlayerShoot.cs
using Unity.Netcode;
using UnityEditor;
using UnityEngine;
[RequireComponent(typeof(WeaponManager))]
public class PlayerShoot : NetworkBehaviour {
private const string PLAYER_TAG = "Player";
[SerializeField] private Camera playerCamera;
[SerializeField] private LayerMask mask;
private GameInput gameInput;
private WeaponManager weaponManager;
private PlayerWeaponSO currentWeaponSO;
public override void OnNetworkSpawn() {
gameInput = GetComponent<GameInput>();
weaponManager = GetComponent<WeaponManager>();
if(playerCamera == null) {
Debug.LogError("PlayerShoot: No camera is detected on the player");
this.enabled = false;
}
}
private void Update() {
if(!IsOwner) {
return;
}
currentWeaponSO = weaponManager.GetCurrentWeaponSO();
if(gameInput.GetPlayerInputActions().PlayerShoot.LMB.triggered) {
ShootServerRpc();
}
}
// Is called on the server when a Player shoots
[ServerRpc]
private void OnShootServerRpc() {
DoShootEffectClientRpc();
}
// Is called on all clients when we need to do a shoot effect
[ClientRpc]
private void DoShootEffectClientRpc() {
weaponManager.GetWeapon().weaponParticleSystem.Play();
}
[ServerRpc(RequireOwnership=false)]
private void ShootServerRpc() {
if(!IsLocalPlayer) {
Debug.Log("Not a local player...RETURNING");
return;
}
// We are shooting, call the OnShootServer method on the server....
// OnShootServerRpc();
Debug.Log("SHOOT...");
RaycastHit hit;
if(Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, currentWeaponSO.weaponRange, mask)) {
if(hit.collider.tag == PLAYER_TAG) {
NetworkObject hitPlayerNetworkObject = hit.collider.GetComponent<NetworkObject>();
if(hitPlayerNetworkObject != null) {
PlayerShotClientRpc(hitPlayerNetworkObject.NetworkObjectId.ToString(), currentWeaponSO.weaponDamage);
}
}
}
}
[ClientRpc]
private void PlayerShotClientRpc(string shotPlayerId, int weaponDamage) {
Debug.Log(shotPlayerId + " has been shot");
Player _player = MpsGameManager.Instance.GetPlayer(shotPlayerId);
_player.TakeDamageClientRpc(currentWeaponSO.weaponDamage);
}
}